I am writting firmware for AT90CAN32 using AVR Studio + WinAVR.
It seems simple for experienced AVR programmers.
I pass int argument to function, but inside the function it is always zero.
There is inside protocol.c file:
#include <uart.h>
static char packet[16] = ":AA111122223!f\r\n";
void vDivRequesting(void)
{
uint8_t cntOfBms = getCountOfBms( );
for(uint8_t i=0; i<cntOfBms; i++)
{
createPacket(getBmsNetworkNumber(i), REQUEST_V_DIV, 1);
usartSendString(packet,11);
_delay_ms(200);
}
}
There is uart.c file:
void usartSendString( char *data, uint8_t len )
{
uint8_t i=0;
usartSendByte(len + 0x30);
for( i=0; i<len; i++)
{
usartSendByte(*data);
*data++;
}
}
And it prints me that len argument is zero. usartSendString function is defined in uart.h file. What is wrong with that code?
Related
Currently I am having issues with a program running a freertos program. The purpose of the program is to control a stepper motor as well as an led. Implementing the motor control without microstepping does not have any issues as the two tasks take no parameters and call no functions.
However, when I introduce microstepping which requires two nested functions to be called by the move_routine task, the program will not do anything (no led flashing, no motor turning) when it did before. Does anyone have any solutions for this or any reasons on why this shouldn't work? From what I can see it should be fine to call a function from a freertos task.
#include <Arduino.h>
#include <Stepper.h>
/*================PIN DEFINITIONS================*/
#define LEDC_CHANNEL_0 0
#define LEDC_CHANNEL_1 1
#define LEDC_CHANNEL_2 2
#define LEDC_CHANNEL_3 3
const int A1A = 14;
const int A1B = 27;
const int B1A = 26;
const int B2A = 25;
const int ledPin = 33;
/*================VARIABLE DEFINITIONS================*/
int stepnumber = 0;
int Pa; int Pb;
const int stepsPerRev = 200;
Stepper myStepper(stepsPerRev, 14,27,26,25);
/*================Function Definitions================*/
//Analogwrite using LEDC capabilities
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
//calculation of duty cycle
uint32_t duty = (4095/valueMax)*min(value, valueMax);
ledcWrite(channel,duty);
}
void move(int stepnumber, int MAXpower, int wait) {
Pa = (sin(stepnumber*0.098174)*MAXpower);
Pb = (cos(stepnumber*0.098174)*MAXpower);
if (Pa>0)
{
ledcAnalogWrite(LEDC_CHANNEL_0,Pa);
ledcAnalogWrite(LEDC_CHANNEL_1,0);
}
else
{
ledcAnalogWrite(LEDC_CHANNEL_0,0);
ledcAnalogWrite(LEDC_CHANNEL_1,abs(Pa));
}
if (Pb>0)
{
ledcAnalogWrite(LEDC_CHANNEL_2,Pb);
ledcAnalogWrite(LEDC_CHANNEL_3,0);
}
else
{
ledcAnalogWrite(LEDC_CHANNEL_2,0);
ledcAnalogWrite(LEDC_CHANNEL_3,abs(Pb));
}
}
void move_routine(void *parameters) {
while(1) {
for (int i=0; i<3199; i++)
{
stepnumber++;
move(stepnumber,255,250);
}
vTaskDelay(3000/portTICK_PERIOD_MS);
for (int i=0; i<1599; i++)
{
stepnumber--;
move(stepnumber,255,1000);
}
vTaskDelay(3000/portTICK_PERIOD_MS);
}
}
void led_task(void * parameters){
while(1){
digitalWrite(ledPin, HIGH);
vTaskDelay(500/portTICK_PERIOD_MS);
digitalWrite(ledPin, LOW);
vTaskDelay(500/portTICK_PERIOD_MS);
}
}
void setup(){
myStepper.setSpeed(60);
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
xTaskCreatePinnedToCore(
move_routine,
"Move Routine",
8192,
NULL,
1,
NULL,
1
);
xTaskCreatePinnedToCore(
led_task,
"LED Task",
1024,
NULL,
1,
NULL,
1
);
}
void loop(){
}
Expected to see flashing led and motor turning but nothing witnessed
Your two tasks work as it were defined, the problem is your ledcAnalogWrite() function where you are calling ledcWrite() function with nowhere in your code initialise the LEDC with ledcSetup() and ledcAttachPin().
Read the documentation on LED Control(LEDC).
The error is generated in this part of the code and precisely here:
writeVal[j] = RBF1[i+1];
I am attaching the code:
#define _FPGA_UPLOAD_C_
#include "globdefs.h"
extern SPI_HandleTypeDef hspi1;
#define MEMORY_ADDR1 ((uint32_t)0x64000000) //FMC_BANK1_2
const uint8_t RBF1[] ={0xA0,0xFF,0x02,0x6A,0xE7,0x82,0xF7,0x07,0xE7,0xF7,0xE7,0xF1,0xFB,0xF1,0xF9,0x82,0xFB,0x83,0xF9,0x08};
uint16_t* fpgaRegx1 = (uint16_t*)FMC_BANK1_1; // FMC_BANK1_1: 0x60000000UL
int8_t FPGA_Loading(char TypeRBF)
{
HAL_GPIO_WritePin(NCONFIG_CYCLONE_GPIO_Port,NCONFIG_CYCLONE_Pin, GPIO_PIN_RESET);
Delay3micro();
HAL_GPIO_WritePin(NCONFIG_CYCLONE_GPIO_Port,NCONFIG_CYCLONE_Pin, GPIO_PIN_SET);
Delay3micro();
int i=0;
uint8_t* writeVal = (uint8_t*)(MEMORY_ADDR1 + sizeof(RBF1));
uint16_t loadOK = 0;
while (hspi1.State != HAL_SPI_STATE_READY)
{};
while (i < sizeof(RBF1))
{
uint8_t n = RBF1[i] & 0x7f;
if (RBF1[i] & 0x80)
{
for (int j=0; j<n; j++){
writeVal[j] = RBF1[i+1];
}
i += 2;
}
else
{
for (int j=0; j<n; j++)
writeVal[j] = RBF1[i+j+1];
i += (n+1);
}
if(HAL_SPI_Transmit (&hspi1, (uint8_t *) writeVal, n, 100)!=HAL_OK)
{
Error_Handler();
}
}
Delay3micro();
Delay3micro();
HAL_SPI_MspDeInit(&hspi1); //spengo l'spi usata per la programmazione dell'fpga
loadOK = fpgaRegx1[0];
loadOK &= 0xF000;
if (loadOK == (uint16_t)0xA000)
{
loadOK=1;
}
else
{
loadOK=0;
//Error_Handler();
}
return loadOK;
}
I tried to generate a new, simpler project by inserting only the code I entered above and I don't get any errors. At this point I have always added on the new project through CubeIDE FILEX, THREADX and SDMMC1 the latter generates the same error written above. For FILEX, THREADX and SDMMC1 the code is only the one generated by CubeIDE.
I've created an array of structs but I'm getting the error written on the title of this question. I'm still new to this so I was wondering if I could get some help.
Code:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16, 2);
i = 0;
}
#define LIMIT 27
struct protocol {
char create[16];
char character;
int values;
int minimum;
int maximum;
};
struct protocol channels[LIMIT];
int i;
void create_channels() {
if (Serial.available() > 0) {
Serial.print("Enter the channel description");
channels[i].create = Serial.read();
Serial.print("Enter the starting character: ");
channels[i].character = Serial.read();
if (i == LIMIT) {
for (i = 0; i < LIMIT; i++)
{
Serial.println(channels[i].create);
Serial.println(channels[i].character);
}
i = 0;
}
}
}
Error:
cw.ino:24:38: error: incompatible types in assignment of 'int' to 'char [16]'
channels[i].create = Serial.read();
I want to transfer a function to calculate the row of a text file.
The compile can pass but the function can not be transferred. I want to know what happens.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int getLine( const char *filename)
{
ifstream infile(filename,ios::in);
if(!infile){
cout<<"can not open"<<filename<<'\n';
return 0;
}
int count=0;
infile.unsetf(ios::skipws);
char buff[300];
while(infile.getline(buff,300))
count++;
cout<<"the total line:"<<count<<endl;
infile.close();
return 0;
}
int getLineNoEmpty(const char* filename)
{
ifstream infile(filename,ios::in);
if(!infile){
cout<<"can not open"<<filename<<'\n';
return 0;
}
int count=0;
char buff[300];
while(infile.getline(buff,300))
{
if(sizeof(buff)==0)
continue;
else
count++;
}
cout<<"the total line without null string:"<<count<<endl;
return 0;
}
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
int getLine(const char &filename);
int getLineNoEmpty(const char &filename);
return 0;
}
The compile can pass but the function can not be transferred. I want to know what happens about it. It can output the result I want. And I don't know how to
realize the goal of calculating the total line without null string.
Firstly, You are just declaring 2 functions in main() without using them. Change
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
int getLine(const char &filename);
int getLineNoEmpty(const char &filename);
return 0;
}
to
int main()
{
char filename[256];
cout<<"input filename:";
cin>>filename;
getLine(filename);
getLineNoEmpty(filename);
return 0;
}
Looking at the following example,
#include <atomic>
extern std::atomic<int> TheVal;
extern std::atomic<bool> Ready;
int Unrelated;
void test() {
for (int i=0; i<100; ++i) {
Unrelated = 42; // A loop-invariant store
if (Ready.load(std::memory_order_acquire)) continue;
TheVal.store(i, std::memory_order_release);
Ready.store(1, std::memory_order_release);
}
}
Is the compiler allowed to move the store to 'Unrelated' out of the loop such that test() would be similar to the following?
void test() {
Unrelated = 42; // Move the loop-invariant store here
for (int i=0; i<100; ++i) {
if (Ready.load(std::memory_order_acquire)) continue;
TheVal.store(i, std::memory_order_release);
Ready.store(1, std::memory_order_release);
}
}
If the variables 'Ready' and 'TheVal' were not atomic then this optimization would be surely safe, but does their atomicity prevent this optimization?