• Non ci sono risultati.

/* struct used for managing the data of the communication */

typedef struct {

unsigned char data [DIM_BUFFER];

/* chars buffer used with a circular nature */

unsigned int head; /* pointer to the first item of the buffer */

unsigned int tail; /* pointer to

the last item of the buffer */

unsigned int counterElements;

}queue;

/*************************************

Defines

**********************************/

#define BAUDRATE 115200

/* example other choices: 9600, 19200, 38400 */

/*********************************

Prototypes for exported functions

**********************************/

void send_data(char[],unsigned int);

int queue_is_empty();

int queue_is_full();

void queue_clear(queue*);

int str_length(char string[]);

#endif

bike uart.c

#include "bike_uart.h"

/*******************************************************

Private global variables

********************************************************/

/* Rx and Tx queue element used for managing the reception data buffer and transmission data buffer */

queue rx_queue;

* Function name : interrupt_tx_init

* Description : Initialize the TXI6 interrupt of SCI6

* Arguments : none

* Return Value : none

******************************************************/

void interrupt_tx_init() {

IR(SCI6,TXI6)=1;

IEN(SCI6, TXI6) = 1;

}

/*******************************************************

* Function name : interrupt_rx_init

* Description : Initialize the RXI6 interrupt of SCI6

* Arguments : none

* Return Value : none

*******************************************************/

void interrupt_rx_init() {

IEN(SCI6, RXI6) = 1; /* Enable the interrupt in the ICU */

}

/*************************************************************

* Function name : rx_queue_init

* Description : Initialize the rx queue setting the pointer

to the head and the tail of the

* rx data buffer

* Arguments : none

* Return Value : none

**************************************************************/

/* Empty queue conditions: head points to the

same element pointed by tail and counterElements=0 */

}

/************************************************************

* Function name : tx_queue_init

* Description : Initialize the tx queue setting the pointer to the head and the tail of the

* tx data buffer

* Arguments : none

* Return Value : none

*************************************************************/

/* Empty queue conditions: head points to the same element pointed by tail and counterElements=0 */

}

/**********************************************************

* Function name : uart_init

* Description : Set up all the registers for enabling UART communication

* Arguments : none

* Return Value : none

***********************************************************/

void uart_init() {

int timeout=0;

/* Enable the UART peripheral to operate */

//Lo UART o Universal

Asynchronous Receiver-Transmitter

//(ricevitore-trasmettitore asincrono universale) //`e un dispositivo hardware,

//di uso generale o dedicato,

//che converte flussi di bit di dati da un formato //parallelo a un formato seriale asincrono o viceversa.

/* (the peripheral must be enabled before being used).*/

/* The SCI12 channel is used because the TxD12 and RxD12 register are accessible by JN2’s pins.

(ref. Hardware Manual Chapt.22 page 703)*/

/* To enable SCId (c=0..12) */

/* the register MSTP(SCId) must be activated, because*/

/*poich´e questo registro `e protetto in scrittura dal registro di protezione PRCR prima di attivare MSTP (MTUi)

`

e necessario:

/*a)settare il PRCR

/*b) abilitare il canale MTUi (i = 1..4) desiderato (cio`e MTU4, i = 4)

/*c) settare sul PRCR

*/

/* Remove peripherals protection */

#ifdef PLATFORM_BOARD_RDKRX63N

SYSTEM.PRCR.WORD = 0xA50B; /* Protect off */

#endif

MSTP(SCI6)=0; /* cancel stop state of SCI12 peripheral to enable writing to it */

/* for SCI12 PE1 is TxD12 and PE2 is RxD12 */

#ifdef PLATFORM_BOARD_RDKRX63N

SYSTEM.PRCR.WORD=0xA500; /* Protect on*/

/* Protect set on (ref. Hardware Manual Chapt.13.1.1)*/

/* write A5 (in hexadecimal) to the eight higher-order */

/* bits and 0B (in hexadecimal) to the eight lower-order */

/* where B in hexadecimal is equivalent to 1011 in Binary */

/* therefore it sets PRC3, PRC1 and PRC0 to 1 */

#endif

/* Set up the UART I/O port and pins */

//MPC.PE2PFS.BIT.ISEL=1; /*alternative code: ISEL is the interrupt (ref. Hardware Manual Chapt.22.2.16 pag 734)*/

//MPC.PE2PFS.BIT.PSEL=0xC; /*This value of PSEL is

for RxD12 */

MPC.P33PFS.BYTE=0x4A; /*P23 of JN2 is RxD12*/

MPC.P32PFS.BYTE=0x4A; /*P22 of JN2 TxD12*/

PORT3.PDR.BIT.B2=1; /* TxD12 is output */

PORT3.PDR.BIT.B3=0; /* RxD12 is input */

PORT3.PMR.BIT.B2=1; /*TxD12 is pheripheral */

PORT3.PMR.BIT.B3=1; /* RxD12 is a pheripheral */

/* Set data transfer format in Serial Mode Register (SMR) - HW 35.2.5

* -Asynchronous Mode

* -8 bits

SCI6.SCMR.BIT.SMIF=0; /* Set to 0 for serial communications

interface mode */

SCI6.BRR=48000000 / ((64/2) * 115200) -1;

/*The baud rate chosen is 115200 equals to the baud rate of the wifi module */

/* Clear IR bits (of the interrupt) for SCI12 registers:

TXI, RXI, TEI */

IR(SCI6,RXI6)=0; //Clear any pending ISR IR(SCI6,TXI6)=0;

IR(SCI6,TEI6)=0;

IPR(SCI6,RXI6)=4;/* Set interrupt priority, not using the transmit end interrupt */

IPR(SCI6,TXI6)=4;

/* Disable all the interrupt (TXI,RXI,TEI) */

IEN(SCI6,TXI6)=0;

IEN(SCI6,RXI6)=0;

IEN(SCI6,TEI6)=0;

/* Clear SCR register: clear bits TIE, RE and TEIE in SCR to 0.

Set CKE to internal */

SCI6.SCR.BYTE=0x00;

/* Enable RXI and TXI interrupts in SCI peripheral -(ref. Hardware Manual cp. 35.2.6) */

SCI6.SCR.BIT.RIE=1; //Reception interrupt request (RXI12) is enabled setting the bit to 1

SCI6.SCR.BIT.TIE=1; //Transmission interrupt request

(TXI12) is enabled setting the bit to 1 SCI6.SCR.BIT.TEIE=0; //A TEI (Transmit End Interrupt) interrupt

request is disabled, not used SCI6.SCR.BIT.TE=1; //Serial transmission is enabled setting

//bit to 1

SCI6.SCR.BIT.RE=1; //Serial reception is enabled //setting bit to 1

/* Initializing the Tx and Rx queues */

rx_queue_init();

tx_queue_init();

/* Sending the setting command string to the wifi module */

// send_data(WIFI_COMMAND1,str_length(WIFI_COMMAND1));

// while (timeout++<=500000){}

/*Waiting for response before of sending the next command */

// send_data(WIFI_COMMAND2,str_length(WIFI_COMMAND2));

// timeout=0;

// while (timeout++<=500000){}

// send_data(WIFI_COMMAND3,str_length(WIFI_COMMAND3));

LED15=LED_ON; /* Led 15 means that the uart init has been completed */

/* Calling the initialization of interrupt for the communication */

* Function name: SCI6_TXI6_int

* Description : Interrupt Service Routine for TXD6.

It is fired when the TSR register of SCI6 is empty.

* Argument : none

* Return value : none

****************************************************/

#pragma interrupt SCI6_TXI6_int (vect = VECT_SCI6_TXI6, enable) static void SCI6_TXI6_int()

{

if (!queue_is_empty(tx_queue_pointer)) /* if the tx queue is not empty */

{

SCI6.TDR=tx_queue_pointer->data[tx_queue_pointer->head];

/* putting into the data buffer*/

tx_queue_pointer->head++; /* Now the head of the buffer will be the next element */

tx_queue_pointer->counterElements--;

if (tx_queue_pointer->counterElements==0) /* Disable

the tx interrupt if all the elements have been transmitted */

{

interrupt_tx_disable();

queue_clear(tx_queue_pointer); /* Clear the tx queue after the transmission */

} } }

/***************************************************

* Function name: SCI6_RXI6_int

* Description : Interrupt Service Routine for RXD6.

It is fired when received data are stored in RDR

* Argument : none

* Return value : none

****************************************************/

#pragma interrupt SCI6_RXI6_int (vect = VECT_SCI6_RXI6, enable) static void SCI6_RXI6_int()

{

int timeout=0;

uint8_t read_byte = SCI6.RDR;

/* Read a byte from the UART data register. */

/* Frame and Parity Error handling */

while(SCI6.SSR.BIT.ORER && timeout++<=10000) {

SCI6.SSR.BIT.ORER=0;

/* Putting the data from RDR register into the reception buffer */

rx_queue_pointer->data[rx_queue_pointer->tail]= read_byte;

rx_queue_pointer->counterElements++;

/* Incrementing the counter of received elements */

rx_queue_pointer->tail++; /* Incrementing the tail pointer to the next position (it will point to an empty element) */

/* if the rx queue is full or last element of the rx queue is the ’\n’ we can start processing the received packet */

if (queue_is_full(rx_queue_pointer) ||

rx_queue_pointer->data[rx_queue_pointer->tail-1]==’\n’) /* The wifi module sends the received command to renesas

with ’\n’ in the final position */

{

// char command[DIM_COMMAND]=split(rx_queue_pointer);

/* splitting the received data from wifi module with putty*/

char command[DIM_COMMAND]=split_matlab(rx_queue_pointer);

clearCommand(); /* clear the command array for being ready for the next communication */

/* it is not necessary to clear the header because it is overwritten every time that a packet (correct or incorrect) is received */

interrupt_rx_disable(); /* Disabling the rx interrupt in order to clear the rx queue completely without the risk of getting other packets */

queue_clear(rx_queue_pointer);

}

/* Enabling again the rx interrupt for being ready for receiving the next packet */

interrupt_rx_init();

}

/**************************************************

* Function name : queue_is_full

* Description : Check if a queue is full

* Arguments : queue pointer to check

* Return Value : int value --> 1 the queue is full, 0 the queue is not full

****************************************************/

int queue_is_full(queue* queue_pointer) {

if (queue_pointer->counterElements>=DIM_BUFFER) /* If there is a number of elements equal to the buffer’s dimension

the buffer is full */

return 1;

else return 0;

}

/****************************************************

* Function name : queue_is_empty

* Description : Check if a queue is empty

* Arguments : queue pointer to check

* Return Value : int value --> 1 the queue is empty, 0 the queue is not empty

*****************************************************/

int queue_is_empty(queue* queue_pointer) {

/*The queue is empty when countElements is 0 */

if (!queue_pointer->counterElements)

* Function name : send_data

* Description :

* Arguments : char array to transmit and its dimension

* Return Value : none

*********************************************************/

void send_data(char data[], unsigned int dim_string) {

unsigned int i=0; /* index for the cycle for inserting data into buffer */

if (dim_string<(DIM_BUFFER-tx_queue_pointer->counterElements)) /* if there are sufficients empty slots */

for (i=0;i<dim_string;i++)

/* putting each char of data array into the tx buffer */

{

tx_queue_pointer->data[(tx_queue_pointer->head)+i]=data[i];

/* Putting elements from head index */

tx_queue_pointer->counterElements++;

tx_queue_pointer->tail++;

if (tx_queue_pointer->tail>=DIM_BUFFER)

/* Managing the tx buffer in a circular way */

tx_queue_pointer->tail=0;

}

interrupt_tx_init();

}

/************************************************

* Function name : interrupt_tx_disable

* Description : Disable TXI6 interrupt of SCI6

* Arguments :

* Return Value :

*************************************************/

void interrupt_tx_disable() {

IR(SCI6,TXI6)=0;

IEN(SCI6, TXI6) = 0;

}

/************************************************

* Function name : interrupt_rx_disable

* Description : Disable RXI8 interrupt of SCI6

* Arguments :

* Return Value :

*************************************************/

void interrupt_rx_disable() {

IEN(SCI6, RXI6) = 0;

}

/********************************************************

* Function name : str_length

* Description : Returns the length of the input string

* Arguments : String

* Return Value : int value -> Length of the string

*********************************************************/

int str_length(char string[]) {

unsigned int i=0;

while(i<DIM_BUFFER)

* Function name : queue_clear

* Description : Clears the buffer of the queue

* Arguments : queue*

* Return Value : none

*************************************************/

void queue_clear(queue* queue_pointer) {

int i;

for(i=0; i<queue_pointer->tail; i++) /* Clear all the bits of the queue */

Documenti correlati