Replies: 2 comments
-
I don't have time today to look at this in detail and try to replicate, but my first guess is that you've run out of RAM, and somewhere a stack is being overwritten. The ATmega328 doesn't have much RAM to go around. If you have a ATmega2650, then try that to see if the problem doesn't show. Also try being more conservative with some of the stack sizes for your null tasks (HouseKeep, SerialReceive, ModeSwitch) currently, say around 96 bytes. See if that changes anything. Unrelated but similar, I once tried to build a NFC sniffer, and write the results to a SD card. There was enough RAM to do either thing, but not both at the same time. That was the inspiration for the ATmega1284p Goldilocks, with 16kB of RAM. |
Beta Was this translation helpful? Give feedback.
-
As you said I was running out of the stack. I didn't mount an LED on pin 13 to get a visual indication.Sorry my bad. |
Beta Was this translation helpful? Give feedback.
-
Code 1
/---------------------------------#includes---------------------------------/
#include <SPI.h>
#include <Wire.h>
#include <Arduino_FreeRTOS.h>
#include <queue.h>>
/--------------------------------#defines-----------------------------------/
#define PIN_TEMP_SENSOR A0
#define PIN_TEST_ANA A3
#define PIN_RELAY_GND 5
#define PIN_RELAY_EXT 6
#define PIN_SPI_CS_DAC 9
#define PIN_SPI_CS_ADC 10
#define ADC_VREF 2500 // 2500mV
#define ADDR_DIGITAL_POT 0x29
#define I2C_CLK_RATE 400000
#define SERIAL_BAUD_RATE 9600
/*
*/
#define QUEUE_SERIAL_SEND_DEPTH 10
#define QUEUE_SERIAL_SEND_BYTE_MAX_SIZE 5
#define TASK_SERIAL_SEND_STACK 128
#define TASK_SERIAL_SEND_PRIORITY 2
#define TASK_SERIAL_RECV_STACK 128
#define TASK_SERIAL_RECV_PRIORITY 1
#define TASK_VIT_READ_STACK 128
#define TASK_VIT_READ_PRIORITY 3
#define TASK_HOUSE_KEEP_STACK 128
#define TASK_HOUSE_KEEP_PRIORITY 0
#define TASK_MODE_SWITCH_STACK 128
#define TASK_MODE_SWITCH_PRIORITY 0
/--------------------------------Global Variables---------------------------/
/*
*/
QueueHandle_t SerialDataQueue;
/----------------------------------Functions--------------------------------/
void setup()
{
/*
*/
pinMode(PIN_SPI_CS_ADC, OUTPUT);
digitalWrite(PIN_SPI_CS_ADC, HIGH);
pinMode(PIN_SPI_CS_DAC, OUTPUT);
digitalWrite(PIN_SPI_CS_DAC, HIGH);
/*
*/
SPI.begin();
SPI.setBitOrder(MSBFIRST); //MSB goes first out
SPI.setClockDivider(SPI_CLOCK_DIV8); //2Mhz SPI clock
/*
*/
// Wire.begin();
// Wire.setClock(I2C_CLK_RATE);
/*
*/
Serial.begin(SERIAL_BAUD_RATE);
SerialDataQueue = xQueueCreate(QUEUE_SERIAL_SEND_DEPTH,
QUEUE_SERIAL_SEND_BYTE_MAX_SIZE);
if (SerialDataQueue != NULL)
{
xTaskCreate(TaskSerialSend, "Serial Data Send Task",
TASK_SERIAL_SEND_STACK, NULL, TASK_SERIAL_SEND_PRIORITY,
NULL);
xTaskCreate(TaskSerialReceive, "Serial Recv Command Task",
TASK_SERIAL_RECV_STACK, NULL, TASK_SERIAL_RECV_PRIORITY,
NULL);
xTaskCreate(TaskVITRead, "Read Volt Current Temp Task",
TASK_VIT_READ_STACK, NULL, TASK_VIT_READ_PRIORITY, NULL);
xTaskCreate(TaskHouseKeep, "House Keeping Task", TASK_HOUSE_KEEP_STACK,
NULL, TASK_HOUSE_KEEP_PRIORITY, NULL);
xTaskCreate(TaskModeSwitch, "Mode Switching Task", TASK_MODE_SWITCH_STACK,
NULL, TASK_MODE_SWITCH_PRIORITY, NULL);
}
}
/---------------------------------------------------------------------------/
void loop()
{
}
/---------------------------------------------------------------------------/
//void writePot(uint8_t address, uint8_t potChannel, uint16_t val)
//{
// Wire.beginTransmission(address);
// Wire.write((potChannel & 3) << 4 | ((val >> 8) & 3));
// Wire.write(val & 0xFF);
// Wire.endTransmission();
//}
/---------------------------------------------------------------------------/
/**
/
void TaskSerialSend(void * pvParameters)
{
uint8_t buf[6];
/*
*/
while (!Serial)
{
vTaskDelay(1);
}
for(;;)
{
if (xQueueReceive(SerialDataQueue, buf, portMAX_DELAY) == pdPASS)
{
Serial.write(buf, QUEUE_SERIAL_SEND_BYTE_MAX_SIZE);
}
}
}
/---------------------------------------------------------------------------/
/**
*/
void TaskVITRead(void * pvParameters)
{
char sTemperature[QUEUE_SERIAL_SEND_BYTE_MAX_SIZE]={'T','0',0x00,0x00,'\n'};
char sADC_V[QUEUE_SERIAL_SEND_BYTE_MAX_SIZE]={'V','0',0x00,0x00,'\n'};
char sADC_C[QUEUE_SERIAL_SEND_BYTE_MAX_SIZE]={'C','0',0x00,0x00,'\n'};
uint16_t *pTemperature = (uint16_t *)&(sTemperature[2]);
for(;;)
{
vTaskSuspendAll(); // Suspend task switching until sensor read is complete.
/Read Temperature/
pTemperature = analogRead(PIN_TEMP_SENSOR);
/
Byte 1.On Sending Byte 1 response will be 'X','X','X','X','X','X','X','X' where X is dont
care.
Byte 1 send definition
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
|b7:0|b6:0|b5:0|b4:0|b3:0|b2:1 start bit|b1:1 single/diff|b0:0 dnt care|
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Byte 2.On Sening Byte 2 response is 'X','X','X','N','B11','B10','B9','B8' where N is null
byte and B11 is signaficant bit and so on.
Byte 2 send defenition
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
|b7:chan_s1|b6:chan_s0|b5:0 dnt care|b4:0 dnt care|b3:0 dnt care|b2:0 dnt care|b1:0 dnt care|b0:0 dnt care|
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Byte 3.On Sending Byte 3 response is 'B7','B6','B5','B4','B3','B2','B1','B0'
Byte 3 send definition
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
|b7:0 dnt care|b6:0 dnt care|b5:0 dnt care|b4:0 dnt care|b3:0 dnt care|b2:0 dnt care|b1:0 dnt care|b0:0 dnt care|
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
,,,,,,,,,,,,,,,,,,,,,,,,,
|chan_s1|chan_s0|channel|
'''''''''''''''''''''''''
| 0 | 0 | 0 |
'''''''''''''''''''''''''
| 0 | 1 | 1 |
'''''''''''''''''''''''''
| 1 | 0 | 2 |
'''''''''''''''''''''''''
| 1 | 1 | 3 |
'''''''''''''''''''''''''
*/
/Read Voltage/
digitalWrite(PIN_SPI_CS_ADC, LOW);
SPI.transfer(0x18); //Refer the above commment for detail of command byte.
sADC_V[3] = SPI.transfer(0x00); // First byte would be MSB from ADC channel 0
sADC_V[2] = SPI.transfer(0x00); // Second byte would be LSB from ADC
digitalWrite(PIN_SPI_CS_ADC, HIGH);
digitalWrite(PIN_SPI_CS_ADC, LOW);
SPI.transfer(0x18);
sADC_C[3] = SPI.transfer(0x40);// First byte would be MSB from ADC channel 1
sADC_C[2] = SPI.transfer(0x00);
digitalWrite(PIN_SPI_CS_ADC, HIGH);
xTaskResumeAll(); // Resume normal operation as sensor read is completed
xQueueSend(SerialDataQueue, &sTemperature, portMAX_DELAY);
xQueueSend(SerialDataQueue, &sADC_V, portMAX_DELAY);
xQueueSend(SerialDataQueue, &sADC_C, portMAX_DELAY);
vTaskDelay(1);
}
}
/---------------------------------------------------------------------------/
void TaskSerialReceive(void * pvParameters)
{
for(;;)
{
vTaskDelay(1000);
}
}
/---------------------------------------------------------------------------/
void TaskHouseKeep(void * pvParameters)
{
for(;;)
{
vTaskDelay(1000);
}
}
/---------------------------------------------------------------------------/
void TaskModeSwitch(void * pvParameters)
{
for(;;)
{
vTaskDelay(1000);
}
}
/---------------------------------------------------------------------------/
Code 2
#include <SPI.h>
#include <Wire.h>
#define QUEUE_SERIAL_SEND_DEPTH 10
#define QUEUE_SERIAL_SEND_BYTE_MAX_SIZE 5
#define TASK_SERIAL_SEND_STACK 128
#define TASK_SERIAL_SEND_PRIORITY 2
#define TASK_DATA_SEND_STACK 128
#define TASK_DATA_SEND_PRIORITY 1
#define SERIAL_BAUD_RATE 9600
#include <Arduino_FreeRTOS.h>
#include <queue.h>>
QueueHandle_t SerialDataQueue;
void setup() {
Serial.begin(SERIAL_BAUD_RATE);
SerialDataQueue = xQueueCreate(QUEUE_SERIAL_SEND_DEPTH,
QUEUE_SERIAL_SEND_BYTE_MAX_SIZE);
if (SerialDataQueue != NULL)
{
xTaskCreate(TaskSerialSend, "Serial Data Send Task",
TASK_SERIAL_SEND_STACK, NULL, TASK_SERIAL_SEND_PRIORITY,
NULL);
xTaskCreate(TaskDataSend, "Serial Data Send Task",
TASK_DATA_SEND_STACK, NULL, TASK_DATA_SEND_PRIORITY,
NULL);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void TaskSerialSend(void * pvParameters)
{
uint8_t buf[6];
while (!Serial)
{
vTaskDelay(1);
}
for (;;)
{
if (xQueueReceive(SerialDataQueue, buf, portMAX_DELAY) == pdPASS)
{
Serial.write(buf, QUEUE_SERIAL_SEND_BYTE_MAX_SIZE);
}
}
}
void TaskDataSend(void * pvParameters)
{
uint8_t buf[] = "HAI00";
while (!Serial)
{
vTaskDelay(1);
}
for (;;)
{
xQueueSend(SerialDataQueue, buf, portMAX_DELAY);
}
}
I was trying to interface atmega328 with MCP4651T Digital pot working with I2C(code 1). I find weird that including Wire.h make my scheduler stopped working. The device is working fine without any problem with Code 2. I don't know what is causing this issue. Could you please assist me.
Beta Was this translation helpful? Give feedback.
All reactions