Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f10x.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_usart.h"
- #include "scmRTOS.h"
- #include "uart.h"
- #include "main.h"
- #include "ws2812b_conf.h"
- #include "Settings.h"
- char rx_buffer1[RX_BUFFER_SIZE2];
- char tx_buffer1[TX_BUFFER_SIZE2];
- uint16_t tx_wr_index1,tx_rd_index1,tx_counter1;
- uint8_t uart_fl1;
- #if RX_BUFFER_SIZE2<256
- unsigned char rx_wr_index1,rx_rd_index1,rx_counter1;
- #else
- unsigned int rx_wr_index1,rx_rd_index1,rx_counter1;
- #endif
- /*
- * USART3 interrupt
- */
- extern "C" OS_INTERRUPT void USART1_IRQHandler(void){
- OS::scmRTOS_ISRW_TYPE ISR;
- uint32_t u1Data, u1State;
- u1State=USR1; u1Data=UDR1;
- /*receive*/
- if (u1State&USART_SR_RXNE){
- rx_buffer1[rx_wr_index1]=u1Data;
- if(u1Data==0x0d){
- efUART1.signal();
- }
- if(++rx_wr_index1 == RX_BUFFER_SIZE2) rx_wr_index1=0;
- if(++rx_counter1 == RX_BUFFER_SIZE2) rx_counter1=0;
- }
- /*transmit*/
- /*
- if(u1State&USART_SR_TC){
- if(tx_counter1){
- --tx_counter1;
- UDR1=tx_buffer1[tx_rd_index1];
- if(++tx_rd_index1 == TX_BUFFER_SIZE2) tx_rd_index1=0;
- }
- else{
- if(uart_fl1&ALL_DATA_SEND) {
- RS485E3.Off();
- LED.On();
- }
- }
- USR1&=~USART_SR_TC;
- }
- */
- }
- /*
- * RS485 init
- */
- void uart_init(void){
- RCC->APB2ENR|= RCC_APB2ENR_AFIOEN;
- RCC->APB2ENR|= RCC_APB2ENR_USART1EN;
- //tx_pin
- GPIOA->CRH |= GPIO_CRH_MODE9; /* TX pin clean*/
- GPIOA->CRH &=~(GPIO_CRH_CNF9); GPIOA->CRH |=GPIO_CRH_CNF9_1; /*set TX pin to PA.9 */
- //rx_pin
- GPIOA->CRH &=~GPIO_CRH_MODE10;
- GPIOA->CRH &=~(GPIO_CRH_CNF10); GPIOA->CRH |=GPIO_CRH_CNF10_1;
- DUART1->CR1 |=(USART_CR1_RE | USART_CR1_TE);
- DUART1->BRR=(72<<4); /*set boud rate*/
- DUART1->CR1 |=USART_CR1_UE; /*interrupt TC, RXNE.*/
- USR1&=~USART_SR_TC; /*clean TC interrupt flag*/
- DUART1->CR1 |=USART_CR1_TCIE|USART_CR1_RXNEIE; /*interrupt TC, RXNE.*/
- NVIC_SetPriority (USART1_IRQn, 1); NVIC_EnableIRQ(USART1_IRQn); /*set and enable USART1 interrupt*/
- }
- /*
- * HEX to ASCII converter
- * input: 1 byte hex value
- * output: ASCII value
- */
- uint8_t hex_to_ASCII (uint8_t hex_to_ASCII_temp){
- hex_to_ASCII_temp&=0x0F;
- if(hex_to_ASCII_temp>9)
- {
- hex_to_ASCII_temp+=7;
- }
- return hex_to_ASCII_temp+=0x30;
- }
- /*
- * ASCII to HEX converter
- * input:
- * ASCII_to_hex_temp1 - first byte ASCII value
- * ASCII_to_hex_temp2 - second byte ASCII value
- * output:
- * HEX value
- */
- uint8_t ASCII_to_hex (uint8_t ASCII_to_hex_temp1, uint8_t ASCII_to_hex_temp2){
- ASCII_to_hex_temp1-=0x30;
- if(ASCII_to_hex_temp1>9)
- {
- ASCII_to_hex_temp1-=7;
- }
- ASCII_to_hex_temp2-=0x30;
- if(ASCII_to_hex_temp2>9)
- {
- ASCII_to_hex_temp2-=7;
- }
- return ((ASCII_to_hex_temp1<<4)&0xF0)+(ASCII_to_hex_temp2&0x0F);
- }
- const char LCD_to_ASCII[] ={"АБВГДЕЖЗИЙКЛMHOПРСТУФХЦЧШЩЪЫЬЭЮЯaбвгдeжзийклмнопрстуфхцчшщъыьэюя"};
- /*
- * LCD to ASCII converter
- * input: 1 byte hex value
- * output: result value
- */
- char LcdToASCII(char value){
- uint8_t n;
- if((value>=0x80)&&(value<0xE0)){
- n=value-0x80;
- return (char)LCD_to_ASCII[n];
- }
- else {
- if((value>=0xE0)&&(value<0xF0)){
- n=value-0xB0;
- return LCD_to_ASCII[n];
- }
- else{
- return value;
- }
- }
- return 0;
- }
- /*
- * HEX to bcd converter
- * input: 1 byte hex value
- * output: result value
- */
- uint8_t hex_bcd(uint8_t h1){
- uint8_t h2;
- h2=h1+((h1/10)*6);
- return(h2);
- }
- /*
- * calcul ELECON CRC
- * input:
- * adr - 1 byte addr value
- * cmd - 1 byte cmd value
- * data - pointer to data array
- * size - size of data array
- */
- uint8_t calc_CRC(uint8_t adr,uint8_t cmd,uint8_t* data,uint8_t size){
- uint8_t temp_CRC,i;
- temp_CRC=0;
- temp_CRC=hex_to_ASCII((adr&0xf0)>>4);
- temp_CRC+=hex_to_ASCII(adr&0x0f);
- temp_CRC+=cmd;
- for(i=0;i<(size);i++)
- {
- temp_CRC+=*data;
- data++;
- }
- return(temp_CRC);
- }
- /*
- * calcul ELECON CRC
- * input:
- * tt - 1 byte first data byte value
- * adr - 1 byte addr value
- * cmd - 1 byte cmd value
- * data - pointer to data array
- * size - size of data array
- */
- uint8_t calc_CRC(uint8_t tt,uint8_t adr,uint8_t cmd,uint8_t* data,uint8_t size){
- uint8_t temp_CRC,i;
- temp_CRC=0;
- temp_CRC=hex_to_ASCII((adr&0xf0)>>4);
- temp_CRC+=hex_to_ASCII(adr&0x0f);
- temp_CRC+=cmd;
- temp_CRC+=tt;
- for(i=0;i<(size);i++)
- {
- temp_CRC+=*data;
- data++;
- }
- return(temp_CRC);
- }
- /*
- * put char value in USART3 transmitter buffer
- * intput: 1 byte char value
- */
- void putchar1(char c){
- while (tx_counter1 == TX_BUFFER_SIZE2);
- while (!(USR1 & USART_SR_TXE)) {}
- if (tx_counter1 )
- {
- tx_buffer1[tx_wr_index1]=c;
- if (++tx_wr_index1 == TX_BUFFER_SIZE2)
- tx_wr_index1=0;
- ++tx_counter1;
- }
- else UDR1=c;
- }
- /*
- * RS485 transmitter
- * input: pointer to data struct
- */
- void request_u1(uint8_t* s_out){
- uint8_t i,s_size,s_adr,s_cmd;
- s_size=*s_out;
- s_adr=*(s_out+1);
- s_cmd=*(s_out+2);
- static uint8_t temp_CRC;
- tx_wr_index1=0;
- tx_rd_index1=0;
- RS485E3.On();
- LED.Off();
- uart_fl1&=~ALL_DATA_SEND;
- #if UART1_MASTER
- putchar1('#');
- #else
- putchar1('!');
- #endif
- putchar1(hex_to_ASCII((s_adr&0xf0)>>4));
- putchar1(hex_to_ASCII(s_adr&0x0f));
- putchar1(s_cmd);
- if(s_cmd=='d'){
- for (i=0;i<(s_size-3);i++){
- putchar1(hex_to_ASCII((*(s_out+i+3)&0xf0)>>4));
- putchar1(hex_to_ASCII(*(s_out+i+3)&0x0f));
- }
- putchar1('k');
- temp_CRC=calc_CRC(s_adr,s_cmd,(s_out+3),(s_size-3));
- }
- else{
- if(s_cmd=='S'){
- for (i=0;i<(s_size-3);i++)
- putchar1(*(s_out+i+3));
- putchar1('k');
- temp_CRC=calc_CRC(s_adr,s_cmd,(s_out+3),(s_size-3));
- }
- }
- putchar1(hex_to_ASCII((temp_CRC&0xf0)>>4));
- putchar1(hex_to_ASCII(temp_CRC&0x0f));
- putchar1(0x0d);
- uart_fl1|=ALL_DATA_SEND;
- }
- /*
- * Get a char from the USART3 Receiver buffer
- * output: 1 byte char data
- */
- char getchar1(void){
- char data;
- if(rx_counter1!=0){
- data=rx_buffer1[rx_rd_index1];
- if (++rx_rd_index1 == RX_BUFFER_SIZE2) rx_rd_index1=0;
- --rx_counter1;
- }
- else data=0;
- return data;
- }
- /*
- * first channel RS485 receiver
- * input:
- * s_in - pointer to LCD data struct
- * lamp_in - pointer to lamp data struct
- * fl - pointer to cmd byte
- * output: message status
- * 1 - req/answ error
- * 2 - addr error
- * 6 - cmd error
- * 10 - CRC error
- * 0x20 - LCD message ok
- * 0x38...0x40 - LED message ok //0x38, 0x39, 0x3A, ..., 0x40
- */
- uint8_t check_u1(uint8_t* s_in,uint8_t* lamp_in, uint8_t *fl){
- uint8_t temp_char1,temp_char2,temp_CRC,stat_u0;
- uint8_t i,s_size,s_cmd;
- temp_char1=0;
- uint8_t buff;
- s_size=*s_in;
- *fl=0;
- s_cmd='d';
- stat_u0=8;
- i=s_size;
- while ((temp_char1!='#')&&(i))
- {
- temp_char1=getchar1();
- i--;
- }
- if((temp_char1=='#')){
- temp_char1=getchar1();
- temp_char2=getchar1();
- buff=ASCII_to_hex(temp_char1,temp_char2);
- if(buff==0x20
- #if LED_NUMBER>0
- ||buff==0x30
- #if LED_NUMBER>1
- || buff==0x31
- #if LED_NUMBER>2
- || buff==0x32
- #if LED_NUMBER>3
- || buff==0x33
- #if LED_NUMBER>4
- || buff==0x34
- #if LED_NUMBER>5
- || buff==0x35
- #if LED_NUMBER>6
- || buff==0x36
- #if LED_NUMBER>7
- || buff==0x37
- #endif
- #endif
- #endif
- #endif
- #endif
- #endif
- #endif
- #endif
- ){
- temp_char1=getchar1();
- /*LCD DATA*/
- if(temp_char1=='S'){
- for (i=0;i<=(s_size-3);i++){
- temp_char1=getchar1();
- if(temp_char1=='k' && rx_buffer1[rx_rd_index1+2]==0x0D){
- temp_char1=getchar1();
- temp_char2=getchar1();
- rx_wr_index1=rx_rd_index1=rx_counter1=0;
- return 0x20;
- }
- else{
- *fl+=1;
- if(CP866_CODE_PAGE_OFF || i<2) *(s_in+3+i)=temp_char1;
- else *(s_in+3+i)=LcdToASCII(temp_char1);
- }
- }
- }
- #if LED_NUMBER>0
- else { /*LED DATA*/
- if(temp_char1=='d'){
- temp_char1=getchar1();
- temp_char2=getchar1();
- *fl=ASCII_to_hex(temp_char1,temp_char2);
- for (i=0;i<=(s_size-3);i++){
- temp_char1=getchar1();
- if(temp_char1=='k'){
- temp_char1=getchar1();
- temp_char2=getchar1();
- temp_CRC=ASCII_to_hex(temp_char1,temp_char2);
- if(temp_CRC==calc_CRC(*fl,buff,s_cmd,lamp_in,80)){
- rx_wr_index1=rx_rd_index1=rx_counter1=0;
- return buff;
- }
- else stat_u0=10;
- }
- else{
- temp_char2=getchar1();
- *(lamp_in+i)=ASCII_to_hex(temp_char1,temp_char2);
- }
- }
- }
- else stat_u0=6;
- }
- #else
- else stat_u0=6;
- #endif
- }
- else stat_u0=2;
- }
- else stat_u0=1;
- if(stat_u0!=20
- #if LED_NUMBER>0
- || buff!=0x30
- #if LED_NUMBER>1
- || buff!=0x31
- #if LED_NUMBER>2
- || buff!=0x32
- #if LED_NUMBER>3
- || buff!=0x33
- #if LED_NUMBER>4
- || buff!=0x34
- #if LED_NUMBER>5
- || buff!=0x35
- #if LED_NUMBER>6
- || buff!=0x36
- #if LED_NUMBER>7
- || buff!=0x37
- #endif
- #endif
- #endif
- #endif
- #endif
- #endif
- #endif
- #endif
- ){
- rx_wr_index1=rx_rd_index1=rx_counter1=0;
- }
- return(stat_u0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement