Advertisement
2andnot

Untitled

Jan 2nd, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.96 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "stm32f10x_gpio.h"
  3. #include "stm32f10x_rcc.h"
  4. #include "stm32f10x_usart.h"
  5. #include "scmRTOS.h"
  6.  
  7. #include "uart.h"  
  8. #include "main.h"
  9. #include "ws2812b_conf.h"
  10. #include "Settings.h"
  11.  
  12. char rx_buffer1[RX_BUFFER_SIZE2];
  13. char tx_buffer1[TX_BUFFER_SIZE2];
  14.  
  15. uint16_t tx_wr_index1,tx_rd_index1,tx_counter1;
  16. uint8_t uart_fl1;
  17. #if RX_BUFFER_SIZE2<256
  18. unsigned char rx_wr_index1,rx_rd_index1,rx_counter1;
  19. #else
  20. unsigned int rx_wr_index1,rx_rd_index1,rx_counter1;
  21. #endif
  22. /*
  23.  * USART3 interrupt
  24.  */
  25. extern "C" OS_INTERRUPT void USART1_IRQHandler(void){
  26.     OS::scmRTOS_ISRW_TYPE ISR;
  27.     uint32_t u1Data, u1State;
  28.     u1State=USR1; u1Data=UDR1;
  29.  
  30.     /*receive*/
  31.     if (u1State&USART_SR_RXNE){
  32.         rx_buffer1[rx_wr_index1]=u1Data;
  33.         if(u1Data==0x0d){
  34.             efUART1.signal();
  35.         }
  36.         if(++rx_wr_index1 == RX_BUFFER_SIZE2) rx_wr_index1=0;
  37.         if(++rx_counter1 == RX_BUFFER_SIZE2) rx_counter1=0;
  38.     }
  39.  
  40.     /*transmit*/
  41.     /*
  42.     if(u1State&USART_SR_TC){
  43.         if(tx_counter1){
  44.             --tx_counter1;
  45.             UDR1=tx_buffer1[tx_rd_index1];
  46.             if(++tx_rd_index1 == TX_BUFFER_SIZE2) tx_rd_index1=0;
  47.         }
  48.         else{
  49.             if(uart_fl1&ALL_DATA_SEND) {
  50.                 RS485E3.Off();
  51.                 LED.On();
  52.             }
  53.         }
  54.         USR1&=~USART_SR_TC;
  55.     }
  56.     */
  57. }
  58.  
  59. /*
  60.  * RS485 init
  61.  */
  62. void uart_init(void){
  63.     RCC->APB2ENR|= RCC_APB2ENR_AFIOEN;
  64.     RCC->APB2ENR|= RCC_APB2ENR_USART1EN;
  65.     //tx_pin
  66.     GPIOA->CRH |= GPIO_CRH_MODE9;                                       /* TX pin clean*/
  67.     GPIOA->CRH &=~(GPIO_CRH_CNF9); GPIOA->CRH |=GPIO_CRH_CNF9_1;        /*set TX pin to PA.9 */
  68.     //rx_pin
  69.     GPIOA->CRH &=~GPIO_CRH_MODE10;
  70.     GPIOA->CRH &=~(GPIO_CRH_CNF10); GPIOA->CRH |=GPIO_CRH_CNF10_1;
  71.  
  72.     DUART1->CR1 |=(USART_CR1_RE | USART_CR1_TE);
  73.     DUART1->BRR=(72<<4);                                                /*set boud rate*/
  74.     DUART1->CR1 |=USART_CR1_UE;                                         /*interrupt TC, RXNE.*/
  75.     USR1&=~USART_SR_TC;                                                 /*clean TC interrupt flag*/
  76.     DUART1->CR1 |=USART_CR1_TCIE|USART_CR1_RXNEIE;                      /*interrupt TC, RXNE.*/
  77.  
  78.     NVIC_SetPriority (USART1_IRQn, 1); NVIC_EnableIRQ(USART1_IRQn);     /*set and enable USART1 interrupt*/
  79. }
  80.  
  81. /*
  82.  * HEX to ASCII converter
  83.  * input: 1 byte hex value
  84.  * output: ASCII value
  85.  */
  86. uint8_t hex_to_ASCII (uint8_t hex_to_ASCII_temp){
  87.     hex_to_ASCII_temp&=0x0F;
  88.     if(hex_to_ASCII_temp>9)
  89.     {
  90.         hex_to_ASCII_temp+=7;
  91.     }
  92.     return hex_to_ASCII_temp+=0x30;
  93. }
  94.  
  95. /*
  96.  * ASCII to HEX converter
  97.  * input:
  98.  *  ASCII_to_hex_temp1 - first byte ASCII value
  99.  *  ASCII_to_hex_temp2 - second byte ASCII value
  100.  * output:
  101.  *  HEX value
  102.  */
  103. uint8_t ASCII_to_hex (uint8_t ASCII_to_hex_temp1, uint8_t ASCII_to_hex_temp2){
  104.     ASCII_to_hex_temp1-=0x30;
  105.     if(ASCII_to_hex_temp1>9)
  106.     {
  107.         ASCII_to_hex_temp1-=7;
  108.     }
  109.     ASCII_to_hex_temp2-=0x30;
  110.     if(ASCII_to_hex_temp2>9)
  111.     {
  112.         ASCII_to_hex_temp2-=7;
  113.     }
  114.     return ((ASCII_to_hex_temp1<<4)&0xF0)+(ASCII_to_hex_temp2&0x0F);
  115. }
  116.  
  117. const char LCD_to_ASCII[] ={"АБВГДЕЖЗИЙКЛMHOПРСТУФХЦЧШЩЪЫЬЭЮЯaбвгдeжзийклмнопрстуфхцчшщъыьэюя"};
  118.  
  119. /*
  120.  * LCD to ASCII converter
  121.  * input: 1 byte hex value
  122.  * output: result value
  123.  */
  124. char LcdToASCII(char value){
  125.     uint8_t n;
  126.     if((value>=0x80)&&(value<0xE0)){
  127.         n=value-0x80;
  128.         return (char)LCD_to_ASCII[n];
  129.     }
  130.     else {
  131.         if((value>=0xE0)&&(value<0xF0)){
  132.             n=value-0xB0;
  133.             return LCD_to_ASCII[n];
  134.         }
  135.         else{
  136.             return value;
  137.         }
  138.     }
  139.     return 0;
  140. }
  141.  
  142. /*
  143.  * HEX to bcd converter
  144.  * input: 1 byte hex value
  145.  * output: result value
  146.  */
  147. uint8_t hex_bcd(uint8_t h1){
  148.     uint8_t h2;
  149.     h2=h1+((h1/10)*6);
  150.     return(h2);
  151. }
  152.  
  153. /*
  154.  * calcul ELECON CRC
  155.  * input:
  156.  *  adr - 1 byte addr value
  157.  *  cmd - 1 byte cmd value
  158.  *  data - pointer to data array
  159.  *  size - size of data array
  160.  */
  161. uint8_t calc_CRC(uint8_t adr,uint8_t cmd,uint8_t* data,uint8_t size){
  162.     uint8_t temp_CRC,i;
  163.     temp_CRC=0;
  164.     temp_CRC=hex_to_ASCII((adr&0xf0)>>4);
  165.     temp_CRC+=hex_to_ASCII(adr&0x0f);
  166.     temp_CRC+=cmd;
  167.     for(i=0;i<(size);i++)
  168.     {
  169.         temp_CRC+=*data;
  170.         data++;
  171.     }
  172.     return(temp_CRC);
  173. }
  174.  
  175. /*
  176.  * calcul ELECON CRC
  177.  * input:
  178.  *  tt - 1 byte first data byte value
  179.  *  adr - 1 byte addr value
  180.  *  cmd - 1 byte cmd value
  181.  *  data - pointer to data array
  182.  *  size - size of data array
  183.  */
  184. uint8_t calc_CRC(uint8_t tt,uint8_t adr,uint8_t cmd,uint8_t* data,uint8_t size){
  185.     uint8_t temp_CRC,i;
  186.     temp_CRC=0;
  187.     temp_CRC=hex_to_ASCII((adr&0xf0)>>4);
  188.     temp_CRC+=hex_to_ASCII(adr&0x0f);
  189.     temp_CRC+=cmd;
  190.     temp_CRC+=tt;
  191.     for(i=0;i<(size);i++)
  192.     {
  193.         temp_CRC+=*data;
  194.         data++;
  195.     }
  196.     return(temp_CRC);
  197. }
  198.  
  199. /*
  200.  * put char value in USART3 transmitter buffer
  201.  * intput: 1 byte char value
  202.  */
  203. void putchar1(char c){
  204.     while (tx_counter1 == TX_BUFFER_SIZE2);
  205.     while (!(USR1  & USART_SR_TXE)) {}
  206.     if (tx_counter1 )
  207.     {
  208.         tx_buffer1[tx_wr_index1]=c;
  209.         if (++tx_wr_index1 == TX_BUFFER_SIZE2)
  210.             tx_wr_index1=0;
  211.         ++tx_counter1;
  212.     }
  213.     else UDR1=c;
  214. }
  215. /*
  216.  * RS485 transmitter
  217.  * input: pointer to data struct
  218.  */
  219. void request_u1(uint8_t* s_out){
  220.     uint8_t i,s_size,s_adr,s_cmd;
  221.     s_size=*s_out;
  222.     s_adr=*(s_out+1);
  223.     s_cmd=*(s_out+2);
  224.     static uint8_t temp_CRC;
  225.     tx_wr_index1=0;
  226.     tx_rd_index1=0;
  227.  
  228.     RS485E3.On();
  229.     LED.Off();
  230.     uart_fl1&=~ALL_DATA_SEND;
  231.  
  232.     #if UART1_MASTER
  233.         putchar1('#');
  234.     #else
  235.         putchar1('!');
  236.     #endif
  237.  
  238.     putchar1(hex_to_ASCII((s_adr&0xf0)>>4));
  239.     putchar1(hex_to_ASCII(s_adr&0x0f));
  240.     putchar1(s_cmd);
  241.     if(s_cmd=='d'){
  242.         for (i=0;i<(s_size-3);i++){
  243.             putchar1(hex_to_ASCII((*(s_out+i+3)&0xf0)>>4));
  244.             putchar1(hex_to_ASCII(*(s_out+i+3)&0x0f));
  245.         }
  246.         putchar1('k');
  247.         temp_CRC=calc_CRC(s_adr,s_cmd,(s_out+3),(s_size-3));
  248.     }
  249.     else{
  250.         if(s_cmd=='S'){
  251.             for (i=0;i<(s_size-3);i++)
  252.                 putchar1(*(s_out+i+3));
  253.  
  254.             putchar1('k');
  255.             temp_CRC=calc_CRC(s_adr,s_cmd,(s_out+3),(s_size-3));
  256.         }
  257.     }
  258.  
  259.     putchar1(hex_to_ASCII((temp_CRC&0xf0)>>4));
  260.     putchar1(hex_to_ASCII(temp_CRC&0x0f));
  261.  
  262.     putchar1(0x0d);
  263.     uart_fl1|=ALL_DATA_SEND;
  264. }
  265.  
  266. /*
  267.  * Get a char from the USART3 Receiver buffer
  268.  * output: 1 byte char data
  269.  */
  270. char getchar1(void){
  271.     char data;
  272.     if(rx_counter1!=0){
  273.         data=rx_buffer1[rx_rd_index1];
  274.         if (++rx_rd_index1 == RX_BUFFER_SIZE2) rx_rd_index1=0;
  275.         --rx_counter1;
  276.     }
  277.     else data=0;
  278.     return data;
  279. }
  280.  
  281. /*
  282.  * first channel RS485 receiver
  283.  * input:
  284.  *  s_in    - pointer to LCD data struct
  285.  *  lamp_in - pointer to lamp data struct
  286.  *  fl      - pointer to cmd byte
  287.  * output: message status
  288.  *  1           - req/answ error
  289.  *  2           - addr error
  290.  *  6           - cmd error
  291.  *  10          - CRC error
  292.  *  0x20        - LCD message ok
  293.  *  0x38...0x40 - LED message ok //0x38, 0x39, 0x3A, ..., 0x40
  294.  */
  295. uint8_t check_u1(uint8_t* s_in,uint8_t* lamp_in, uint8_t *fl){
  296.     uint8_t temp_char1,temp_char2,temp_CRC,stat_u0;
  297.     uint8_t i,s_size,s_cmd;
  298.     temp_char1=0;
  299.     uint8_t buff;
  300.     s_size=*s_in;
  301.     *fl=0;
  302.     s_cmd='d';
  303.     stat_u0=8;
  304.     i=s_size;
  305.  
  306.     while ((temp_char1!='#')&&(i))
  307.     {
  308.         temp_char1=getchar1();
  309.         i--;
  310.     }
  311.  
  312.     if((temp_char1=='#')){
  313.         temp_char1=getchar1();
  314.         temp_char2=getchar1();
  315.         buff=ASCII_to_hex(temp_char1,temp_char2);
  316.         if(buff==0x20
  317. #if LED_NUMBER>0
  318.             ||buff==0x30
  319. #if LED_NUMBER>1
  320.             || buff==0x31
  321. #if LED_NUMBER>2
  322.             || buff==0x32
  323. #if LED_NUMBER>3
  324.             || buff==0x33
  325. #if LED_NUMBER>4
  326.             || buff==0x34
  327. #if LED_NUMBER>5
  328.             || buff==0x35
  329. #if LED_NUMBER>6
  330.             || buff==0x36
  331. #if LED_NUMBER>7
  332.             || buff==0x37
  333. #endif
  334. #endif
  335. #endif
  336. #endif
  337. #endif
  338. #endif
  339. #endif
  340. #endif
  341.             ){
  342.             temp_char1=getchar1();
  343.             /*LCD DATA*/
  344.  
  345.             if(temp_char1=='S'){
  346.  
  347.                 for (i=0;i<=(s_size-3);i++){
  348.                     temp_char1=getchar1();
  349.                     if(temp_char1=='k' && rx_buffer1[rx_rd_index1+2]==0x0D){
  350.                         temp_char1=getchar1();
  351.                         temp_char2=getchar1();
  352.                         rx_wr_index1=rx_rd_index1=rx_counter1=0;
  353.                         return 0x20;
  354.                     }
  355.                     else{
  356.                         *fl+=1;
  357.  
  358.                         if(CP866_CODE_PAGE_OFF || i<2) *(s_in+3+i)=temp_char1;
  359.                         else *(s_in+3+i)=LcdToASCII(temp_char1);
  360.                     }
  361.                 }
  362.             }
  363.  
  364. #if LED_NUMBER>0
  365.             else { /*LED DATA*/
  366.                 if(temp_char1=='d'){
  367.                     temp_char1=getchar1();
  368.                     temp_char2=getchar1();
  369.                     *fl=ASCII_to_hex(temp_char1,temp_char2);
  370.                     for (i=0;i<=(s_size-3);i++){
  371.                         temp_char1=getchar1();
  372.                         if(temp_char1=='k'){
  373.                             temp_char1=getchar1();
  374.                             temp_char2=getchar1();
  375.                             temp_CRC=ASCII_to_hex(temp_char1,temp_char2);
  376.                             if(temp_CRC==calc_CRC(*fl,buff,s_cmd,lamp_in,80)){
  377.                                 rx_wr_index1=rx_rd_index1=rx_counter1=0;
  378.                                 return buff;
  379.                             }
  380.                             else stat_u0=10;
  381.                         }
  382.                         else{
  383.                             temp_char2=getchar1();
  384.                             *(lamp_in+i)=ASCII_to_hex(temp_char1,temp_char2);
  385.                         }
  386.                     }
  387.                 }
  388.                 else stat_u0=6;
  389.             }
  390. #else
  391.             else stat_u0=6;
  392. #endif
  393.         }
  394.         else stat_u0=2;
  395.     }
  396.     else stat_u0=1;
  397.  
  398.     if(stat_u0!=20
  399. #if LED_NUMBER>0
  400.             || buff!=0x30
  401. #if LED_NUMBER>1
  402.             || buff!=0x31
  403. #if LED_NUMBER>2
  404.             || buff!=0x32
  405. #if LED_NUMBER>3
  406.             || buff!=0x33
  407. #if LED_NUMBER>4
  408.             || buff!=0x34
  409. #if LED_NUMBER>5
  410.             || buff!=0x35
  411. #if LED_NUMBER>6
  412.             || buff!=0x36
  413. #if LED_NUMBER>7
  414.             || buff!=0x37
  415. #endif
  416. #endif
  417. #endif
  418. #endif
  419. #endif
  420. #endif
  421. #endif
  422. #endif
  423.             ){
  424.         rx_wr_index1=rx_rd_index1=rx_counter1=0;
  425.     }
  426.  
  427.     return(stat_u0);
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement