We are having a problem with the max_calls settings on PJSIP and Asterisk.
We are stress-testing our Asterisk server, but found that we had a max 32 active call limitation on our PJSIP module. We are using PJSIP to test our Asterisk server
After a quick google we found the following settings would fix the problem.
Following steps can be taken to increase number of calls supported on PJSIP:
Example: If you have to increase simultaneous calls to 1000 change the following:
1. Change PJSUA_MAX_CALLS to 1000 and PJSUA_MAX_ACC to 1000
2. Change PJ_IOQUEUE_MAX_HANDLES to 2000 (double of desired number of calls).
3. Change __FD_SETSIZE to double to 2000 (double of desired number of calls).
4. Change PJSUA_MAX_PLAYERS to 1000.
5. Recompile pjsip using following steps:
a. ./configure --disable-ssl --disable-sound;
b. make dep
c. make
d. make install
6. Recompile your application with new libs.
Somehow This is not working for us;
What are we doing wrong here?
Anybody suggestions. Help would be higly appreciated.
Our code in the config_site.php file
/*
* This file contains several sample settings especially for Windows
* Mobile and Symbian targets. You can include this file in your
* <pj/config_site.h> file.
*
* The Windows Mobile and Symbian settings will be activated
* automatically if you include this file.
*
* In addition, you may specify one of these macros (before including
* this file) to activate additional settings:
*
* #define PJ_CONFIG_NOKIA_APS_DIRECT
* Use this macro to activate the APS-Direct feature. Please see
* http://trac.pjsip.org/repos/wiki/Nokia_APS_VAS_Direct for more
* info.
*
* #define PJ_CONFIG_WIN32_WMME_DIRECT
* Configuration to activate "APS-Direct" media mode on Windows or
* Windows Mobile, useful for testing purposes only.
*/
/*
* Typical configuration for WinCE target.
*/
#if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE!=0
/*
* PJLIB settings.
*/
/* Disable floating point support */
#define PJ_HAS_FLOATING_POINT 0
/*
* PJMEDIA settings
*/
/* Select codecs to disable */
#define PJMEDIA_HAS_L16_CODEC 0
#define PJMEDIA_HAS_ILBC_CODEC 0
/* We probably need more buffers on WM, so increase the limit */
#define PJMEDIA_SOUND_BUFFER_COUNT 32
/* Fine tune Speex's default settings for best performance/quality */
#define PJMEDIA_CODEC_SPEEX_DEFAULT_QUALITY 5
/* For CPU reason, disable speex AEC and use the echo suppressor. */
#define PJMEDIA_HAS_SPEEX_AEC 0
/* Previously, resampling is disabled due to performance reason and
* this condition prevented some 'light' wideband codecs (e.g: G722.1)
* to work along with narrowband codecs. Lately, some tests showed
* that 16kHz <-> 8kHz resampling using libresample small filter was
* affordable on ARM9 260 MHz, so here we decided to enable resampling.
* Note that it is important to make sure that libresample is created
* using small filter. For example PJSUA_DEFAULT_CODEC_QUALITY must
* be set to 3 or 4 so pjsua-lib will apply small filter resampling.
*/
//#define PJMEDIA_RESAMPLE_IMP PJMEDIA_RESAMPLE_NONE
#define PJMEDIA_RESAMPLE_IMP PJMEDIA_RESAMPLE_LIBRESAMPLE
/* Use the lighter WSOLA implementation */
#define PJMEDIA_WSOLA_IMP PJMEDIA_WSOLA_IMP_WSOLA_LITE
/*
* PJSIP settings.
*/
/* Set maximum number of dialog/transaction/calls to minimum to reduce
* memory usage
*/
#define PJSIP_MAX_TSX_COUNT 31
#define PJSIP_MAX_DIALOG_COUNT 31
#define PJSUA_MAX_CALLS 64
/*
* PJSUA settings
*/
/* Default codec quality, previously was set to 5, however it is now
* set to 4 to make sure pjsua instantiates resampler with small filter.
*/
#define PJSUA_DEFAULT_CODEC_QUALITY 4
/* Set maximum number of objects to minimum to reduce memory usage */
#define PJSUA_MAX_ACC 64
#define PJSUA_MAX_PLAYERS 64
#define PJSUA_MAX_RECORDERS 4
#define PJSUA_MAX_CONF_PORTS (PJSUA_MAX_CALLS+2*PJSUA_MAX_PLAYERS)
#define PJSUA_MAX_BUDDIES 32
#endif /* PJ_WIN32_WINCE */
/*
* Typical configuration for Symbian OS target
*/
#if defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0
/*
* PJLIB settings.
*/
/* Disable floating point support */
#define PJ_HAS_FLOATING_POINT 0
/* Misc PJLIB setting */
#define PJ_MAXPATH 80
/* This is important for Symbian. Symbian lacks vsnprintf(), so
* if the log buffer is not long enough it's possible that
* large incoming packet will corrupt memory when the log tries
* to log the packet.
*/
#define PJ_LOG_MAX_SIZE (PJSIP_MAX_PKT_LEN+500)
/* Since we don't have threads, log buffer can use static buffer
* rather than stack
*/
#define PJ_LOG_USE_STACK_BUFFER 0
/* Disable check stack since it increases footprint */
#define PJ_OS_HAS_CHECK_STACK 0
/*
* PJMEDIA settings
*/
/* Disable non-Symbian audio devices */
#define PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO 0
#define PJMEDIA_AUDIO_DEV_HAS_WMME 0
/* Select codecs to disable */
#define PJMEDIA_HAS_L16_CODEC 0
#define PJMEDIA_HAS_ILBC_CODEC 0
#define PJMEDIA_HAS_G722_CODEC 0
/* Fine tune Speex's default settings for best performance/quality */
#define PJMEDIA_CODEC_SPEEX_DEFAULT_QUALITY 5
/* For CPU reason, disable speex AEC and use the echo suppressor. */
#define PJMEDIA_HAS_SPEEX_AEC 0
/* Previously, resampling is disabled due to performance reason and
* this condition prevented some 'light' wideband codecs (e.g: G722.1)
* to work along with narrowband codecs. Lately, some tests showed
* that 16kHz <-> 8kHz resampling using libresample small filter was
* affordable on ARM9 222 MHz, so here we decided to enable resampling.
* Note that it is important to make sure that libresample is created
* using small filter. For example PJSUA_DEFAULT_CODEC_QUALITY must
* be set to 3 or 4 so pjsua-lib will apply small filter resampling.
*/
//#define PJMEDIA_RESAMPLE_IMP PJMEDIA_RESAMPLE_NONE
#define PJMEDIA_RESAMPLE_IMP PJMEDIA_RESAMPLE_LIBRESAMPLE
/* Use the lighter WSOLA implementation */
#define PJMEDIA_WSOLA_IMP PJMEDIA_WSOLA_IMP_WSOLA_LITE
/* We probably need more buffers especially if MDA audio backend
* is used, so increase the limit
*/
#define PJMEDIA_SOUND_BUFFER_COUNT 32
/*
* PJSIP settings.
*/
/* Disable safe module access, since we don't use multithreading */
#define PJSIP_SAFE_MODULE 0
/* Use large enough packet size */
#define PJSIP_MAX_PKT_LEN 2000
/* Symbian has problem with too many large blocks */
#define PJSIP_POOL_LEN_ENDPT 1000
#define PJSIP_POOL_INC_ENDPT 1000
#define PJSIP_POOL_RDATA_LEN 2000
#define PJSIP_POOL_RDATA_INC 2000
#define PJSIP_POOL_LEN_TDATA 2000
#define PJSIP_POOL_INC_TDATA 512
#define PJSIP_POOL_LEN_UA 2000
#define PJSIP_POOL_INC_UA 1000
#define PJSIP_POOL_TSX_LAYER_LEN 256
#define PJSIP_POOL_TSX_LAYER_INC 256
#define PJSIP_POOL_TSX_LEN 512
#define PJSIP_POOL_TSX_INC 128
/*
* PJSUA settings.
*/
/* Default codec quality, previously was set to 5, however it is now
* set to 4 to make sure pjsua instantiates resampler with small filter.
*/
#define PJSUA_DEFAULT_CODEC_QUALITY 4
/* Set maximum number of dialog/transaction/calls to minimum */
#define PJSIP_MAX_TSX_COUNT 31
#define PJSIP_MAX_DIALOG_COUNT 31
#define PJSUA_MAX_CALLS 64
/* Other pjsua settings */
#define PJSUA_MAX_ACC 64
#define PJSUA_MAX_PLAYERS 64
#define PJSUA_MAX_RECORDERS 4
#define PJSUA_MAX_CONF_PORTS (PJSUA_MAX_CALLS+2*PJSUA_MAX_PLAYERS)
#define PJSUA_MAX_BUDDIES 32
#endif
/*
* Additional configuration to activate APS-Direct feature for
* Nokia S60 target
*
* Please see http://trac.pjsip.org/repos/wiki/Nokia_APS_VAS_Direct
*/
#ifdef PJ_CONFIG_NOKIA_APS_DIRECT
/* MUST use switchboard rather than the conference bridge */
#define PJMEDIA_CONF_USE_SWITCH_BOARD 1
/* Enable APS sound device backend and disable MDA & VAS */
#define PJMEDIA_AUDIO_DEV_HAS_SYMB_MDA 0
#define PJMEDIA_AUDIO_DEV_HAS_SYMB_APS 1
#define PJMEDIA_AUDIO_DEV_HAS_SYMB_VAS 0
/* Enable passthrough codec framework */
#define PJMEDIA_HAS_PASSTHROUGH_CODECS 1
/* And selectively enable which codecs are supported by the handset */
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU 1
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMA 1
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR 1
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_G729 1
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC 1
#endif
/*
* Additional configuration to activate VAS-Direct feature for
* Nokia S60 target
*
* Please see http://trac.pjsip.org/repos/wiki/Nokia_APS_VAS_Direct
*/
#ifdef PJ_CONFIG_NOKIA_VAS_DIRECT
/* MUST use switchboard rather than the conference bridge */
#define PJMEDIA_CONF_USE_SWITCH_BOARD 1
/* Enable VAS sound device backend and disable MDA & APS */
#define PJMEDIA_AUDIO_DEV_HAS_SYMB_MDA 0
#define PJMEDIA_AUDIO_DEV_HAS_SYMB_APS 0
#define PJMEDIA_AUDIO_DEV_HAS_SYMB_VAS 1
/* Enable passthrough codec framework */
#define PJMEDIA_HAS_PASSTHROUGH_CODECS 1
/* And selectively enable which codecs are supported by the handset */
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU 1
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMA 1
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR 1
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_G729 1
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC 1
#endif
/*
* Configuration to activate "APS-Direct" media mode on Windows,
* useful for testing purposes only.
*/
#ifdef PJ_CONFIG_WIN32_WMME_DIRECT
/* MUST use switchboard rather than the conference bridge */
#define PJMEDIA_CONF_USE_SWITCH_BOARD 1
/* Only WMME supports the "direct" feature */
#define PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO 0
#define PJMEDIA_AUDIO_DEV_HAS_WMME 1
/* Enable passthrough codec framework */
#define PJMEDIA_HAS_PASSTHROUGH_CODECS 1
/* Only PCMA and PCMU are supported by WMME-direct */
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU 1
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMA 1
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR 0
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_G729 0
#define PJMEDIA_HAS_PASSTHROUGH_CODEC_ILBC 0
#endif
/*
* iPhone sample settings.
*/
#if PJ_CONFIG_IPHONE
/*
* PJLIB settings.
*/
/* Both armv6 and armv7 has FP hardware support.
* See https://trac.pjsip.org/repos/ticket/1589 for more info
*/
#define PJ_HAS_FLOATING_POINT 1
/*
* PJMEDIA settings
*/
/* We have our own native CoreAudio backend */
#define PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO 0
#define PJMEDIA_AUDIO_DEV_HAS_WMME 0
#define PJMEDIA_AUDIO_DEV_HAS_COREAUDIO 1
/* The CoreAudio backend has built-in echo canceller! */
#define PJMEDIA_HAS_SPEEX_AEC 0
/* Disable some codecs */
#define PJMEDIA_HAS_L16_CODEC 0
#define PJMEDIA_HAS_G722_CODEC 0
/* Use the built-in CoreAudio's iLBC codec (yay!) */
#define PJMEDIA_HAS_ILBC_CODEC 1
#define PJMEDIA_ILBC_CODEC_USE_COREAUDIO 1
/* Fine tune Speex's default settings for best performance/quality */
#define PJMEDIA_CODEC_SPEEX_DEFAULT_QUALITY 5
/*
* PJSIP settings.
*/
/* Increase allowable packet size, just in case */
//#define PJSIP_MAX_PKT_LEN 2000
/*
* PJSUA settings.
*/
/* Default codec quality, previously was set to 5, however it is now
* set to 4 to make sure pjsua instantiates resampler with small filter.
*/
#define PJSUA_DEFAULT_CODEC_QUALITY 4
/* Set maximum number of dialog/transaction/calls to minimum */
#define PJSIP_MAX_TSX_COUNT 31
#define PJSIP_MAX_DIALOG_COUNT 31
#define PJSUA_MAX_CALLS 64
/* Other pjsua settings */
#define PJSUA_MAX_ACC 64
#define PJSUA_MAX_PLAYERS 64
#define PJSUA_MAX_RECORDERS 4
#define PJSUA_MAX_CONF_PORTS (PJSUA_MAX_CALLS+2*PJSUA_MAX_PLAYERS)
#define PJSUA_MAX_BUDDIES 32
#endif
/*
* Android sample settings.
*/
#if PJ_CONFIG_ANDROID
#define PJ_ANDROID 1
/*
* PJLIB settings.
*/
/* Disable floating point support */
#undef PJ_HAS_FLOATING_POINT
#define PJ_HAS_FLOATING_POINT 0
/*
* PJMEDIA settings
*/
/* We have our own OpenSL ES backend */
#define PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO 0
#define PJMEDIA_AUDIO_DEV_HAS_WMME 0
#define PJMEDIA_AUDIO_DEV_HAS_OPENSL 0
#define PJMEDIA_AUDIO_DEV_HAS_ANDROID_JNI 1
/* Disable some codecs */
#define PJMEDIA_HAS_L16_CODEC 0
#define PJMEDIA_HAS_G722_CODEC 0
/* Fine tune Speex's default settings for best performance/quality */
#define PJMEDIA_CODEC_SPEEX_DEFAULT_QUALITY 5
/*
* PJSIP settings.
*/
/* Increase allowable packet size, just in case */
//#define PJSIP_MAX_PKT_LEN 2000
/*
* PJSUA settings.
*/
/* Default codec quality, previously was set to 5, however it is now
* set to 4 to make sure pjsua instantiates resampler with small filter.
*/
#define PJSUA_DEFAULT_CODEC_QUALITY 4
/* Set maximum number of dialog/transaction/calls to minimum */
#define PJSIP_MAX_TSX_COUNT 31
#define PJSIP_MAX_DIALOG_COUNT 31
#define PJSUA_MAX_CALLS 64
/* Other pjsua settings */
#define PJSUA_MAX_ACC 64
#define PJSUA_MAX_PLAYERS 64
#define PJSUA_MAX_RECORDERS 4
#define PJSUA_MAX_CONF_PORTS (PJSUA_MAX_CALLS+2*PJSUA_MAX_PLAYERS)
#define PJSUA_MAX_BUDDIES 32
#endif
/*
* BB10
*/
#if defined(PJ_CONFIG_BB10) && PJ_CONFIG_BB10
/* Quality 3 - 4 to use resampling small filter */
#define PJSUA_DEFAULT_CODEC_QUALITY 4
#define PJMEDIA_HAS_LEGACY_SOUND_API 0
#undef PJMEDIA_HAS_SPEEX_AEC
#define PJMEDIA_HAS_SPEEX_AEC 0
#undef PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO
#define PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO 0
#endif
/*
* Minimum size
*/
#ifdef PJ_CONFIG_MINIMAL_SIZE
# undef PJ_OS_HAS_CHECK_STACK
# define PJ_OS_HAS_CHECK_STACK 0
# define PJ_LOG_MAX_LEVEL 0
# define PJ_ENABLE_EXTRA_CHECK 0
# define PJ_HAS_ERROR_STRING 0
# undef PJ_IOQUEUE_MAX_HANDLES
/* Putting max handles to lower than 32 will make pj_fd_set_t size smaller
* than native fdset_t and will trigger assertion on sock_select.c.
*/
# define PJ_IOQUEUE_MAX_HANDLES 128
# define PJ_CRC32_HAS_TABLES 0
# define PJSIP_MAX_TSX_COUNT 15
# define PJSIP_MAX_DIALOG_COUNT 15
# define PJSIP_UDP_SO_SNDBUF_SIZE 4000
# define PJSIP_UDP_SO_RCVBUF_SIZE 4000
# define PJMEDIA_HAS_ALAW_ULAW_TABLE 0
#elif defined(PJ_CONFIG_MAXIMUM_SPEED)
# define PJ_SCANNER_USE_BITWISE 0
# undef PJ_OS_HAS_CHECK_STACK
# define PJ_OS_HAS_CHECK_STACK 0
# define PJ_LOG_MAX_LEVEL 3
# define PJ_ENABLE_EXTRA_CHECK 0
# define PJ_IOQUEUE_MAX_HANDLES 5000
# define PJSIP_MAX_TSX_COUNT ((640*1024)-1)
# define PJSIP_MAX_DIALOG_COUNT ((640*1024)-1)
# define PJSIP_UDP_SO_SNDBUF_SIZE (24*1024*1024)
# define PJSIP_UDP_SO_RCVBUF_SIZE (24*1024*1024)
# define PJ_DEBUG 0
# define PJSIP_SAFE_MODULE 0
# define PJ_HAS_STRICMP_ALNUM 0
# define PJ_HASH_USE_OWN_TOLOWER 1
# define PJSIP_UNESCAPE_IN_PLACE 1
# if defined(PJ_WIN32) || defined(PJ_WIN64)
# define PJSIP_MAX_NET_EVENTS 10
# endif
# define PJSUA_MAX_CALLS 512
#endif
To Change the calls limit in PJSIP:
1---
Go to
vim /home/administrator/pjproject-2.8/pjlib/include/pj/config_site.h
#define PJSUA_MAX_CALLS 400
#define PJSUA_MAX_ACC 400
#define PJ_IOQUEUE_MAX_HANDLES 400
#define __FD_SETSIZE 800
#define PJSUA_MAX_PLAYERS 400
2--
Go to
vim pjproject-2.8\pjproject-2.8\pjsip\src\pjsua-lib\pjsua_core.c
cfg->max_calls = ((PJSUA_MAX_CALLS) < 400) ? (PJSUA_MAX_CALLS) : 400;
Related
Good day, I am working with a PIC24FJ64GA002 microcontroller and am struggling to get the PWM working for a servo I want to use. My code is as follows:
// PIC24FJ64GA002 Configuration Bit Settings
// 'C' source line config statements
// CONFIG2
#pragma config POSCMOD = NONE // Primary Oscillator Select (Primary oscillator disabled)
#pragma config I2C1SEL = PRI // I2C1 Pin Location Select (Use default SCL1/SDA1 pins)
#pragma config IOL1WAY = ON // IOLOCK Protection (Once IOLOCK is set, cannot be changed)
#pragma config OSCIOFNC = OFF // Primary Oscillator Output Function (OSC2/CLKO/RC15 functions as CLKO (FOSC/2))
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Clock switching and Fail-Safe Clock Monitor are disabled)
#pragma config FNOSC = FRC // Oscillator Select (Fast RC Oscillator (FRC))
#pragma config SOSCSEL = SOSC // Sec Oscillator Select (Default Secondary Oscillator (SOSC))
#pragma config WUTSEL = LEG // Wake-up timer Select (Legacy Wake-up Timer)
#pragma config IESO = ON // Internal External Switch Over Mode (IESO mode (Two-Speed Start-up) enabled)
// CONFIG1
#pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128)
#pragma config WINDIS = ON // Watchdog Timer Window (Standard Watchdog Timer enabled,(Windowed-mode is disabled))
#pragma config FWDTEN = ON // Watchdog Timer Enable (Watchdog Timer is enabled)
#pragma config ICS = PGx1 // Comm Channel Select (Emulator EMUC1/EMUD1 pins are shared with PGC1/PGD1)
#pragma config GWRP = OFF // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF // General Code Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG port is disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
/*
* File: 34660046LAB2.c
* Author: leone
*
* Created on 06 September 2021, 1:27 PM
*/
#include "p24FJ64GA002.h"
#include "xc.h"
#define _LATR15 OC1R
int main(void) {
T2CON = 0x8010;
TMR2=0;
PR2=9999;
_T2IP=4; //Default priority value
_T2IF=0; //Clears interrupt flag before interrupt
_T2IE=1; //Enables interrupt
OC1CONbits.OC = 0; // Output compare channel is disabled
OC1R = 0x1388 ; // Initialize Compare Register1 with 50% duty cycle
OC1CONbits.OCSIDL = 0; // Output capture will continue to operate in CPU Idle mode
OC1CONbits.OCFLT = 0; // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC1CONbits.OCTSEL = 0; // Timer2 is the clock source for output Compare
OC1CONbits.OCM = 0x6; // PWM mode on OC, Fault pin disabled
TRISBbits.TRISB15=0;
_LATB15=0;
while(1)
{
if(TMR2>OC1R)
{
_LATB15=1;
}
else
{
_LATB15=0;
}
}
return 0;
}
I programmed the T1CON to have a period of 2 ms, and the OC1R to have half that period, which should lead to a duty cycle of 50%. I am using the FRC oscillator (8MHz) and my pre-scaler values were <0,1>. I understand the it's the OC1R pin that gives the period the cycle is high, but in the datasheets they refer to it as a pin, however they don't give what pin it is (i.e. A0,A1,R15 etc.) There is also very little example code I could find explaining the how to code this well. If anyone is a PIC expert of some kind help would be very much appreciated! The datasheet for the MCU can be downloaded at https://www.microchip.com/en-us/product/PIC24FJ64GA002.
First of all configure the IO pins for PWM output, I assume you use SPDIP package and wanna use the OC1 PWM output pin:
// Unlock Registers
__builtin_write_OSCCONL(OSCCON & 0xBF);
// Configure Output Functions (Table 10-3)
// Assign OC1 To Pin RP2
RPOR1bits.RP2R = 18;
// Lock Registers
__builtin_write_OSCCONL(OSCCON | 0x40);
For the output pin configurations see the 10.4.3.2 Output Mapping section of the datasheet.
As per steps in the datasheet's 14.3 Pulse-Width Modulation Mode section:
Set the PWM period by writing to the selected Timery Period register (PRy). İf your period is 2 ms then compute the PRy register value:
#define Fosc ( 8000000 )
#define PWM_PERIOD_MS ( 2 )
#define PWM_FREQ_HZ ( 1000 / PWM_PERIOD_MS ) // In this case 500Hz
#define PRy_VALUE (uint16_t) ( (( Fosc ) / ( 4 * TMRyPS * PWM_FREQ)) - 1 )
// Somewhere in the init func or code assign the computed value for the period register
PRx = PRx_VALUE;
Set the PWM duty cycle by writing to the OCxRS register. Since you want a 50% duty cycle compute the value for OCxRS register:
#define DUTY (50)
#define DUTY_VALUE (uint16_t) ( ( 4 * ( TMRyPS - 1 ) * DUTY ) / 100 )
// Somewhere in the init func or code assign the computed value for the duty cycle register
OCxRS = DUTY_VALUE;
Write the OCxR register with the initial duty cycle. Assign the DUTY_VALUE to the OCxR register right after OCxRS assignment:
OCxR = DUTY_VALUE;
Enable interrupts, if required, for the timer and output compare modules. The output compare interrupt is required for PWM Fault pin utilization.
Configure the output compare module for one of two PWM Operation modes by writing to the Output Compare Mode bits, OCM<2:0>
(OCxCON<2:0>).
OC1CONbits.OCTSEL = 0; // Timer2 is the clock source for output Compare
OC1CONbits.OCM = 0x6; // PWM mode on OC, Fault pin disabled
Set the TMRy prescale value and enable the time base by setting TON (TyCON<15>) = 1.
T2CON = 0x0010 // Timer2 prescaler 1:8, internal clock
T2CON.TON = 1;
From now on you should have the PWM working if the above steps are done correctly. Modify your code as per instructions here. Then try it and let me know the result.
This is a complete application that builds with MPLABX v5.50 and XC16 v1.70:
/*
* File: main.c
* Author: dan1138
* Target: PIC24FJ64GA002
* Compiler: XC16 v1.70
* IDE: MPLABX v5.50
*
* Created on October 8, 2021, 1:12 PM
*
* PIC24FJ64GA002
* +-------------------:_:-------------------+
* ICD_VPP --> : 1 MCLR VDD 28 : <-- 3v3
* < > : 2 RA0/AN0 VSS 27 : <-- GND
* < > : 3 RA1/AN1 AN9/RP15/RB15 26 : < >
* ICD_PGD < > : 4 RB0/PGD1/AN2 AN6/RP14/RB14 25 : < >
* ICD_PGC < > : 5 RB1/PGC1/AN3 AN7/RP13/RB13 24 : < >
* PWM/OC1 < > : 6 RB2/RP2/SDA2/AN4 AN8/RP12/RB12 23 : < >
* < > : 7 RB3/RP3/SCL2/AN5 RP11/RB11 22 : <*>
* - > : 8 VSS RP10/RB10 21 : <*>
* < > : 9 RA2/OSCI VCAP 20 : <-- 10uF
* < > : 10 RA3/OSCO VSS 19 : <-- GND
* < > : 11 RB4/RP4 SDA1/RP9/RB9 18 : <*>
* < > : 12 RA4 SCL1/RP8/RB8 17 : <*>
* 3v3 --> : 13 VDD RP7/RB7 16 : <*>
* <*> : 14 RB5/RP5/PGD3 PGC3/RP6/RB6 15 : <*>
* +-----------------------------------------+
* DIP-28
* * Indicates 5.0 volt tolerant input pins.
*
* Description:
* Initialize the controller to use a system oscillator of 8MHz from the on chip Fast RC oscillator.
* Setup the OC1 function to provide a 4KHz square wave output on GPIO pin RB2.
*
* Notes:
* See: https://stackoverflow.com/questions/69453833/pwm-settings-in-pic24fj64ga002
*
*/
#pragma config POSCMOD = NONE, I2C1SEL = PRI, IOL1WAY = OFF, OSCIOFNC = ON
#pragma config FCKSM = CSECMD, FNOSC = FRC, SOSCSEL = SOSC, WUTSEL = LEG
#pragma config IESO = ON, WDTPS = PS32768, FWPSA = PR128, WINDIS = ON
#pragma config FWDTEN = OFF, ICS = PGx1, GWRP = OFF, GCP = OFF, JTAGEN = OFF
#include "xc.h"
/*
* Define the system oscillator frequency that this code will setup
*/
#define FSYS (8000000ul)
#define FCY (FSYS/2ul)
/*
* Initialize this PIC
*/
void PIC_Init(void) {
/* Disable all interrupt sources */
__builtin_disi(0x3FFF); /* disable interrupts for 16383 cycles */
IEC0 = 0;
IEC1 = 0;
IEC2 = 0;
IEC3 = 0;
IEC4 = 0;
__builtin_disi(0x0000); /* enable interrupts */
CLKDIV = 0x0000; /* set for 8MHz FRC clock operations */
AD1PCFG = 0xffff; /* Set for digital I/O */
CMCON = 0x0000;
_NSTDIS = 1; /* disable interrupt nesting */
TRISA = 0xFFFF;
TRISB = 0xFFFF;
}
/*
* Initialize OC1 for PWM operation on PORTB bit RB2/RP2
*/
void OC1_Init(void) {
OC1CON = 0; /* turn off OC1 */
AD1PCFGbits.PCFG4 = 1; /* make GPIO RB2/RP2/AN4 a digital I/O pin */
LATBbits.LATB2 = 0; /* make GPIO RB2/RP2/AN4 output low */
TRISBbits.TRISB2 = 0; /* make GPIO RB2/RP2/AN4 a digital output */
_RP2R = 18; /* magic number to assign OC1 to RB2, see DS39881E-page 109 */
OC1CONbits.OCTSEL = 0; /* Use TIMER2 clock input and prescaler settings as clock for PWM count register */
OC1CONbits.OCM = 0b110; /* Set OC1 for PWM mode, fault shutdown disabled */
T2CON = 0; /* turn off TIMER2 */
T2CONbits.TCKPS = 0b00; /* set TIMER2 prescale as 1:1 */
T2CONbits.TCS = 0; /* set TIMER2 clock source as FSYS/2 */
PR2 = (FCY/4000ul)-1; /* set PWM period to 4KHz */
OC1RS = (PR2+1)>>1; /* set PWM duty cycle to 50% */
T2CONbits.TON = 1; /* turn on TIMER2 */
}
/*
* main application
*/
int main(void) {
PIC_Init();
OC1_Init();
/*
* application process loop
*/
for(;;) {
/* poll for start of PWM period */
if(IFS0bits.T2IF){
IFS0bits.T2IF= 0; /* put a breakpoint here to verify PWM timing with simulator stop watch */
Nop();
Nop();
Nop();
}
}
return 0;
}
This code does run correctly with the MPLABX simulation tool. This is almost never true. IMHO the MPLABX simulator is crap.
This is screen shot of the simulation session working:
An important note on the PIC24FJ64GA002 output compare module is that it is an early silicon implementation and the newer controllers have independent period count registers for each output compare module. This means that the output compare module periods are not tied to the TIMER period on these controllers. I mention this because it really confused me.
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 ).
#include <stdio.h>
#include <fcntl.h> /* File Control Definitions */
#include <termios.h> /* POSIX Terminal Control Definitions */
#include <unistd.h> /* UNIX Standard Definitions */
#include <errno.h> /* ERROR Number Definitions */
#include <sys/ioctl.h> /* ioctl() */
void main(void)
{
int fd;/*File Descriptor*/
printf("\n +----------------------------------+");
printf("\n | USB To RS485 Write |");
printf("\n +----------------------------------+");
/*------------------------------- Opening the Serial Port -------------------------------*/
/* Change /dev/ttyUSB0 to the one corresponding to your system */
fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY); /* ttyUSB0 is the FT232 based USB2SERIAL Converter */
/* O_RDWR Read/Write access to serial port */
/* O_NOCTTY - No terminal will control the process */
if(fd == -1) /* Error Checking */
printf("\n Error! in Opening ttyUSB0 ");
else
printf("\n ttyUSB0 Opened Successfully ");
/*---------- Setting the Attributes of the serial port using termios structure --------- */
struct termios SerialPortSettings; /* Create the structure */
tcgetattr(fd, &SerialPortSettings); /* Get the current attributes of the Serial port */
cfsetispeed(&SerialPortSettings,B4800); /* Set Read Speed as 9600 */
cfsetospeed(&SerialPortSettings,B4800); /* Set Write Speed as 9600 */
SerialPortSettings.c_cflag &= PARENB; /* Disables the Parity Enable bit(PARENB),So No Parity */
SerialPortSettings.c_cflag &= ~CSTOPB; /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the mask for setting the data size */
SerialPortSettings.c_cflag |= CS7; /* Set the data bits = 8 */
SerialPortSettings.c_cflag &= ~CRTSCTS; /* No Hardware flow Control */
SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines */
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); /* Disable XON/XOFF flow control both i/p and o/p */
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* Non Cannonical mode */
SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/
/* Setting Time outs */
SerialPortSettings.c_cc[VMIN] = 1; /* Read at least 10 character */
SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly */
if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/
printf("\n ERROR ! in Setting attributes");
else
printf("\n BaudRate = 9600 \n StopBits = 1 \n Parity = none");
/*---------------------------------------- Controlling RTS and DTR Pins --------------------------------------*/
/* ~RTS(USB2SERIAL) ---> ~RE(MAX485) */
/* ~DTR(USB2SERIAL) ---> DE(MAX485) */
/*------------------------- Putting MAX485 chip in USB2SERIAL in Transmit Mode ---------------------------*/
// //
// ----+ +-----------+ H +-----------+ //
// | | ~RTS| --------------> |~RE | //
// PC |==========>| FT232 | | MAX485 +(A,B)~~~~~~~~~~~~~~~>Data out(RS485) //
// | USB | ~DTR| --------------> | DE | Twisted Pair //
// ----+ +-----------+ H +-----------+ //
// //
//--------------------------------------------------------------------------------------------------------//
//TxMode - DE->High,~RE -> High
int RTS_flag,DTR_flag;
RTS_flag = TIOCM_RTS; /* Modem Constant for RTS pin */
DTR_flag = TIOCM_DTR; /* Modem Constant for DTR pin */
ioctl(fd,TIOCMBIC,&RTS_flag);/* ~RTS = 1,So ~RE pin of MAX485 is HIGH */
ioctl(fd,TIOCMBIC,&DTR_flag);/* ~DTR = 1,So DE pin of MAX485 is HIGH,Transmit Mode enabled */
/*------------------------------- Write data to serial port -----------------------------*/
tcflush(fd, TCIFLUSH); /* Discards old data in the rx buffer */
char write_buffer[] = "///?01!"; /* Buffer containing characters to write into port */
char write_buffer1[] = {'\r','\n'};
int bytes_written = 0; /* Value for storing the number of bytes written to the port */
bytes_written = write(fd,write_buffer,7);/* use write() to send data to port */
/* "fd" - file descriptor pointing to the opened serial port */
/* "write_buffer" - address of the buffer containing data */
/* "sizeof(write_buffer)" - No of bytes to write */
printf("\n %s written to ttyUSB0",write_buffer);
bytes_written = write(fd,write_buffer1,2);/* use write() to send data to port */
/* "fd" - file descriptor pointing to the opened serial port */
/* "write_buffer" - address of the buffer containing data */
/* "sizeof(write_buffer)" - No of bytes to write */
printf("\n %s written to ttyUSB0",write_buffer);
printf("\n %d Bytes written to ttyUSB0", bytes_written);
printf("\n +----------------------------------+\n\n");
char buf[20000];
sleep(2);
while(1)
{
printf("tring to read port\n");
int res = read(fd,buf,1);
sleep(1);
printf("%c :: %d\n",buf[0],res);
}
printf("\n +----------------------------------+\n\n\n");
close(fd); /* Close the serial port */
}
--------------------------------------------------------
Setting for running the code :BAUD RATE is 4800 Databits is 7 , Parity is EVEN, and Stopbit is 1.
If i compile the code and run it is not working. Before running the code if i open and close the minicom with BAUD B4800 & 7E1 then my code is working.
Can any one suggest where is the problem why it is not working.
I have a RFID-RC522 (MF-RC522) module and I'm using Arduino sketch program. I have downloaded the Example code:
/*
* --------------------------------------------------------------------------------------------------------------------
* Example sketch/program showing how to read data from a PICC to serial.
* --------------------------------------------------------------------------------------------------------------------
* This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
*
* Example sketch/program showing how to read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
* Reader on the Arduino SPI interface.
*
* When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
* then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
* you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
* will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages
* when removing the PICC from reading distance too early.
*
* If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading).
* So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all
* details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so
* keep the PICCs at reading distance until complete.
*
* #license Released into the public domain.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
When I execute it, I get the message:
Firmware Version: 0x0 = (unknown)
WARNING: Communication failure, is the MFRC522 properly connected?
Scan PICC to see UID, SAK, type, and data blocks...
I have re-checked the connections a thousand time but it's not working, the pins are connected as the example says to, and the LED D1 is on (red). Does anyone can help me?
If anybody looking for a solution...
Step-1: Change the RST pin to 5 if you are using Arduino Uno or use RST pin 9 in Arduino Mega. the other pins remains the same.
Step-2:Download AddicoreRFID library.
Step-3: Open the Arduino IDE and navigate to Sketch > Include the Library using “Add .ZIP Library…” option.
Step-4: Restart the Arduino IDE (close and open again)
Step-5: Inside Arduino IDE Navigate to File > Examples > AddicoreRFID. Select the “Addicore_RFID_Example” sketch and upload the sketch
Step-6: Open the Serial window and select "Both NL & CR" and select "9600" for the baud rate option
Detail explanation can be found at link
This issue is due to incorrect wiring or loose contacts to RFID sensor. You need to solder contact to get results.
Also, choose appropriate PINS for your Arduino board.
UNO solution worked with me
Reset Bin is 5 not 10
Use 5V not 3.3v
Arduino forum Solution with explanation
Try this code
#include <RFID.h>
/*
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS/SDA: Pin 10
* RST: Pin 9
*/
#include <SPI.h>
#include <RFID.h>
#define SS_PIN 10
#define RST_PIN 9
RFID rfid(SS_PIN,RST_PIN);
int self = 7;
int bat = 8;
int s = 6;
int serNum[5];
int cards[][5] = {
{42,249,70,213,64}
};
bool access = false;
void setup(){
Serial.begin(9600);
SPI.begin();
rfid.init();
pinMode(self, OUTPUT);
pinMode(bat ,OUTPUT);
pinMode(s,OUTPUT);
digitalWrite(self, LOW);
digitalWrite(bat , LOW);
}
void loop(){
if(rfid.isCard()){
if(rfid.readCardSerial()){
Serial.print(rfid.serNum[0]);
Serial.print(" ");
Serial.print(rfid.serNum[1]);
Serial.print(" ");
Serial.print(rfid.serNum[2]);
Serial.print(" ");
Serial.print(rfid.serNum[3]);
Serial.print(" ");
Serial.print(rfid.serNum[4]);
Serial.println("");
for(int x = 0; x < sizeof(cards); x++){
for(int i = 0; i < sizeof(rfid.serNum); i++ ){
if(rfid.serNum[i] != cards[x][i]) {
access = false;
break;
} else {
access = true;
}
}
if(access) break;
}
}
if(access){
Serial.println("Welcome!");
digitalWrite(bat,HIGH);
digitalWrite(self,HIGH);
delay(1000);
digitalWrite(self,LOW);
} else {
Serial.println("Not allowed!");
digitalWrite(s,HIGH);
delay(2000);
digitalWrite(s,LOW);
digitalWrite(bat,LOW);
}
}
rfid.halt();`enter code here`
}
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.