comm_uart_arduino.cpp 4.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
	Copyright 2016 João Lino	jl@joaolino.com

	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 3 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 for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */

/*
 * comm_uart_arduino.c
 *
 *  Created on: 19 dez 2016
 *      Author: joão
 */

#include <comm_uart_arduino.h>
#include <bldc_interface_uart.h>

service-config's avatar
DONE  
service-config committed
28

29 30
// Settings							
#define __CUA_UART_BAUDRATE			57600 // 57600 was tested to be the max value without errors //115200 // 9600
31 32

// Private functions
service-config's avatar
DONE  
service-config committed
33
static void send_packet(unsigned char *data, int len);
34 35 36 37

// Interrupts (Makeshift "Threads")
static bool is_data_ready = false;

service-config's avatar
DONE  
service-config committed
38 39 40
#ifdef __CUA_USE_SECOND_SERIAL
	SoftwareSerial mySerial(8, 7); // RX, TX
#endif
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

/**
 * This thread is only for calling the timer function once
 * per millisecond. Can also be implementer using interrupts
 * if no RTOS is available.
 */
 ISR(TIMER2_COMPA_vect){//timer1 interrupt 1kHz
	bldc_interface_uart_run_timer();
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
service-config's avatar
DONE  
service-config committed
57 58 59 60 61 62 63 64
void softwareSerialEvent() {
	bool isWork = false;
	#ifdef DEBUG_BLDC
		int len = 0;
	#endif

	if(mySerial.available()) {
		#ifdef DEBUG_BLDC
65
			Serial.println(F("[ bldc ] softwareSerialEvent: Reading from serial..."));
service-config's avatar
DONE  
service-config committed
66
		#endif
67

68
		while (mySerial.available()) {
69 70
			bldc_interface_uart_process_byte(mySerial.read());
			#ifdef DEBUG_BLDC
71
				len += sizeof(uint8_t);
72
			#endif
73
		}
service-config's avatar
DONE  
service-config committed
74

75
		
service-config's avatar
DONE  
service-config committed
76
		#ifdef DEBUG_BLDC
77
			Serial.print(F("[ bldc ] softwareSerialEvent: Just read "));
service-config's avatar
DONE  
service-config committed
78
			Serial.print(len);
79
			Serial.println(F(" bytes."));
service-config's avatar
DONE  
service-config committed
80 81 82
		#endif

		is_data_ready = true;
83
		
service-config's avatar
DONE  
service-config committed
84
	}
85 86 87 88 89 90 91
	else {
		#ifdef DEBUG_BLDC
			Serial.println(F("[ bldc ] softwareSerialEvent: nothing to do... "));
		#endif
	}

	
92 93 94 95 96 97 98 99 100 101
}

/**
 * Callback that the packet handler uses to send an assembled packet.
 *
 * @param data
 * Data array pointer
 * @param len
 * Data array length
 */
102
void send_packet(unsigned char *data, int len) {
103

104
	// Send the data over UART
service-config's avatar
DONE  
service-config committed
105
	#ifdef DEBUG_BLDC
106
		Serial.print(F("[ bldc ] send_packet = len["));
service-config's avatar
DONE  
service-config committed
107
		Serial.print(len);
108 109 110
		Serial.print(F("] write["));
		for (int i = 0; i < len ; i++){
			Serial.print(data[i]);
111
			Serial.print(F(":"));
112
		}
113
		
service-config's avatar
DONE  
service-config committed
114 115
		Serial.println(F("]"));
	#endif
116
	
service-config's avatar
DONE  
service-config committed
117
	mySerial.write(data, len);
118
	delay(1);
119 120 121 122
}

 void comm_uart_arduino_init() {
	// Initialize UART
service-config's avatar
DONE  
service-config committed
123
	mySerial.begin(__CUA_UART_BAUDRATE);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

	// Initialize the bldc interface and provide a send function
	bldc_interface_uart_init(&send_packet);

	//stop interrupts
	noInterrupts();

	/*set timer0 interrupt at 61.1Hz
	TCCR0A = 0;// set entire TCCR0A register to 0
	TCCR0B = 0;// same for TCCR0B
	TCNT0  = 0;//initialize counter value to 0
	// set compare match register for 61.1hz increments
	OCR0A = 255;// = (16*10^6) / (1*1024) - 1 (must be <256)
	// turn on CTC mode
	TCCR0A |= (1 << WGM01);
	// Set CS02 and CS00 bits for 1024 prescaler
	TCCR0B |= (1 << CS02) | (1 << CS00);   
	// enable timer compare interrupt
	TIMSK0 |= (1 << OCIE0A);*/

service-config's avatar
DONE  
service-config committed
144
	/*set timer1 interrupt at 1Hz
145 146 147 148 149 150 151 152 153 154
	TCCR1A = 0;// set entire TCCR1A register to 0
	TCCR1B = 0;// same for TCCR1B
	TCNT1  = 0;//initialize counter value to 0
	// set compare match register for 1hz increments
	OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
	// turn on CTC mode
	TCCR1B |= (1 << WGM12);
	// Set CS101 and CS12 bits for 1024 prescaler
	TCCR1B |= (1 << CS12) | (1 << CS10);  
	// enable timer compare interrupt
service-config's avatar
DONE  
service-config committed
155
	TIMSK1 |= (1 << OCIE1A);*/
156 157

	//set timer2 interrupt at 1kHz
158 159 160
	TCCR2A = 0;		// set entire TCCR2A register to 0
	TCCR2B = 0;		// same for TCCR2B
	TCNT2  = 0;		//initialize counter value to 0
161
	// set compare match register for 1khz increments
162
	OCR2A = 249;	// = (16*10^6) / (8000*8) - 1 (must be <256)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	// turn on CTC mode
	TCCR2A |= (1 << WGM21);
	// Set CS21 bit for 64 prescaler
	TCCR2B |= (1 << CS22);   
	// enable timer compare interrupt
	TIMSK2 |= (1 << OCIE2A);

	// Allow interrupts so that the Processing and Timer interrupts ("Threads") start
	interrupts();
}

/*
ISR(TIMER0_COMPA_vect){//timer0 interrupt 61.1Hz 

}
*/