Teste Flash
Dependencies: pulga-lorawan-drv Si1133 BME280
serial.cpp@70:99b7a15c09da, 2021-09-13 (annotated)
- Committer:
- MatteusCarr
- Date:
- Mon Sep 13 18:55:32 2021 +0000
- Revision:
- 70:99b7a15c09da
- Parent:
- 67:17addc7bf8c2
Teste Flash
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ruschigo | 62:89df9529dbb0 | 1 | #include "serial.h" |
ruschigo | 62:89df9529dbb0 | 2 | |
ruschigo | 62:89df9529dbb0 | 3 | RawSerial pc(P0_28, P0_25); |
ruschigo | 62:89df9529dbb0 | 4 | |
ruschigo | 62:89df9529dbb0 | 5 | static char circ_buffer[512]; |
ruschigo | 62:89df9529dbb0 | 6 | static int rd_index = 0; |
ruschigo | 62:89df9529dbb0 | 7 | static int wr_index = 0; |
ruschigo | 62:89df9529dbb0 | 8 | static int buf_len = 0; |
ruschigo | 62:89df9529dbb0 | 9 | |
ruschigo | 62:89df9529dbb0 | 10 | void SerialRx(void) |
ruschigo | 62:89df9529dbb0 | 11 | { |
pancotinho | 67:17addc7bf8c2 | 12 | char msg[256]; |
ruschigo | 62:89df9529dbb0 | 13 | int msg_incompleta; |
ruschigo | 62:89df9529dbb0 | 14 | int msg_len; |
ruschigo | 62:89df9529dbb0 | 15 | int i, j; |
ruschigo | 62:89df9529dbb0 | 16 | |
ruschigo | 62:89df9529dbb0 | 17 | /// Execucao do comando nao deve fazer parte da recepcao de dados serial! |
ruschigo | 62:89df9529dbb0 | 18 | /// Deve existir separacao entre camada fisica, protocolo de baixo nivel |
ruschigo | 62:89df9529dbb0 | 19 | /// e de alto nivel. |
ruschigo | 62:89df9529dbb0 | 20 | |
ruschigo | 62:89df9529dbb0 | 21 | /// 1) Vamos implementar a parte de recepcao serial com buffer circular. |
ruschigo | 62:89df9529dbb0 | 22 | /// 2) Vamos implementar rejeicao de ruido. |
ruschigo | 62:89df9529dbb0 | 23 | /// 3) Interpretamos, executamos ou descartamos comandos. |
ruschigo | 62:89df9529dbb0 | 24 | |
ruschigo | 62:89df9529dbb0 | 25 | /// Recepcao serial |
ruschigo | 62:89df9529dbb0 | 26 | while(pc.readable()) |
ruschigo | 62:89df9529dbb0 | 27 | { |
ruschigo | 62:89df9529dbb0 | 28 | /// buffer circular cheio entao sai do loop |
ruschigo | 62:89df9529dbb0 | 29 | if (buf_len >= sizeof(circ_buffer)) { |
ruschigo | 62:89df9529dbb0 | 30 | break; |
ruschigo | 62:89df9529dbb0 | 31 | } |
ruschigo | 62:89df9529dbb0 | 32 | circ_buffer[wr_index] = pc.getc(); |
ruschigo | 62:89df9529dbb0 | 33 | |
ruschigo | 62:89df9529dbb0 | 34 | buf_len++; |
ruschigo | 62:89df9529dbb0 | 35 | wr_index++; |
ruschigo | 62:89df9529dbb0 | 36 | |
ruschigo | 62:89df9529dbb0 | 37 | if (wr_index >= sizeof(circ_buffer)) { |
ruschigo | 62:89df9529dbb0 | 38 | wr_index = 0; |
ruschigo | 62:89df9529dbb0 | 39 | } |
ruschigo | 62:89df9529dbb0 | 40 | } |
ruschigo | 62:89df9529dbb0 | 41 | |
ruschigo | 62:89df9529dbb0 | 42 | /// Vamos retirar todos os caracteres invalidos antes do comando |
ruschigo | 62:89df9529dbb0 | 43 | /// Um comando sempre inicia com '<' |
ruschigo | 62:89df9529dbb0 | 44 | while(buf_len > 0) |
ruschigo | 62:89df9529dbb0 | 45 | { |
ruschigo | 62:89df9529dbb0 | 46 | if (circ_buffer[rd_index] == '<') { |
ruschigo | 62:89df9529dbb0 | 47 | break; /// rd_index fica com o indice do inicio da mensagem |
ruschigo | 62:89df9529dbb0 | 48 | } |
ruschigo | 62:89df9529dbb0 | 49 | |
ruschigo | 62:89df9529dbb0 | 50 | buf_len--; |
ruschigo | 62:89df9529dbb0 | 51 | rd_index++; |
ruschigo | 62:89df9529dbb0 | 52 | |
ruschigo | 62:89df9529dbb0 | 53 | if (rd_index >= sizeof(circ_buffer)) { |
ruschigo | 62:89df9529dbb0 | 54 | rd_index = 0; |
ruschigo | 62:89df9529dbb0 | 55 | } |
ruschigo | 62:89df9529dbb0 | 56 | } |
ruschigo | 62:89df9529dbb0 | 57 | |
ruschigo | 62:89df9529dbb0 | 58 | /// Se ficou vazio, tudo era ruido, limpa todas as variaveis globais e sai |
ruschigo | 62:89df9529dbb0 | 59 | if (buf_len <= 0) |
ruschigo | 62:89df9529dbb0 | 60 | { |
ruschigo | 62:89df9529dbb0 | 61 | //printf("ruido\n"); |
ruschigo | 62:89df9529dbb0 | 62 | rd_index = 0; |
ruschigo | 62:89df9529dbb0 | 63 | wr_index = 0; |
ruschigo | 62:89df9529dbb0 | 64 | buf_len = 0; |
ruschigo | 62:89df9529dbb0 | 65 | pc.attach(&serial_post_to_queue, RawSerial::RxIrq); |
ruschigo | 62:89df9529dbb0 | 66 | return; |
ruschigo | 62:89df9529dbb0 | 67 | } |
ruschigo | 62:89df9529dbb0 | 68 | |
ruschigo | 62:89df9529dbb0 | 69 | /// Agora vamos verificar se a mensagem esta completa. |
ruschigo | 62:89df9529dbb0 | 70 | /// Procura o final (terminada em '>'). |
ruschigo | 62:89df9529dbb0 | 71 | /// Nao altero rd_index!! |
ruschigo | 62:89df9529dbb0 | 72 | i = 0; |
ruschigo | 62:89df9529dbb0 | 73 | j = rd_index; |
ruschigo | 62:89df9529dbb0 | 74 | msg_incompleta = 1; |
ruschigo | 62:89df9529dbb0 | 75 | |
ruschigo | 62:89df9529dbb0 | 76 | while (i < buf_len) |
ruschigo | 62:89df9529dbb0 | 77 | { |
ruschigo | 62:89df9529dbb0 | 78 | if (circ_buffer[j] == '>') |
ruschigo | 62:89df9529dbb0 | 79 | { |
ruschigo | 62:89df9529dbb0 | 80 | ///encontrou final de cmd |
ruschigo | 62:89df9529dbb0 | 81 | msg_incompleta = 0; |
ruschigo | 62:89df9529dbb0 | 82 | break; |
ruschigo | 62:89df9529dbb0 | 83 | } |
ruschigo | 62:89df9529dbb0 | 84 | j++; |
ruschigo | 62:89df9529dbb0 | 85 | i++; |
ruschigo | 62:89df9529dbb0 | 86 | |
ruschigo | 62:89df9529dbb0 | 87 | if (j >= sizeof(circ_buffer)) { |
ruschigo | 62:89df9529dbb0 | 88 | j = 0; |
ruschigo | 62:89df9529dbb0 | 89 | } |
ruschigo | 62:89df9529dbb0 | 90 | } |
ruschigo | 62:89df9529dbb0 | 91 | |
ruschigo | 62:89df9529dbb0 | 92 | /// Se a mensagem esta incompleta, deixa para a proxima vez. |
ruschigo | 62:89df9529dbb0 | 93 | /// Mas aqui tem uma sacada, se o buffer circular estiver cheio, |
ruschigo | 62:89df9529dbb0 | 94 | /// nunca receberei o final... Entao o codigo nao receberia nunca mais. |
ruschigo | 62:89df9529dbb0 | 95 | /// Para evitar o travamento permanente, limpa tudo nesse caso. |
ruschigo | 62:89df9529dbb0 | 96 | /// <------------ Isso causava o erro !!!!!! |
ruschigo | 62:89df9529dbb0 | 97 | if (msg_incompleta) |
ruschigo | 62:89df9529dbb0 | 98 | { |
ruschigo | 62:89df9529dbb0 | 99 | if (buf_len >= sizeof(circ_buffer)) |
ruschigo | 62:89df9529dbb0 | 100 | { |
ruschigo | 62:89df9529dbb0 | 101 | rd_index = 0; |
ruschigo | 62:89df9529dbb0 | 102 | wr_index = 0; |
ruschigo | 62:89df9529dbb0 | 103 | buf_len = 0; |
ruschigo | 62:89df9529dbb0 | 104 | } |
ruschigo | 62:89df9529dbb0 | 105 | pc.attach(&serial_post_to_queue, RawSerial::RxIrq); |
ruschigo | 62:89df9529dbb0 | 106 | return; |
ruschigo | 62:89df9529dbb0 | 107 | } |
ruschigo | 62:89df9529dbb0 | 108 | |
ruschigo | 62:89df9529dbb0 | 109 | /// Finalmente copio a msg |
ruschigo | 62:89df9529dbb0 | 110 | msg_len = 0; |
ruschigo | 62:89df9529dbb0 | 111 | while(buf_len > 0) |
ruschigo | 62:89df9529dbb0 | 112 | { |
ruschigo | 62:89df9529dbb0 | 113 | if (msg_len < sizeof(msg)) |
ruschigo | 62:89df9529dbb0 | 114 | { |
ruschigo | 62:89df9529dbb0 | 115 | msg[msg_len] = circ_buffer[rd_index]; |
ruschigo | 62:89df9529dbb0 | 116 | msg_len++; |
ruschigo | 62:89df9529dbb0 | 117 | } |
ruschigo | 62:89df9529dbb0 | 118 | else |
ruschigo | 62:89df9529dbb0 | 119 | { |
ruschigo | 62:89df9529dbb0 | 120 | /// A mensagem esta errada, grande demais. |
ruschigo | 62:89df9529dbb0 | 121 | msg_len = 0; |
ruschigo | 62:89df9529dbb0 | 122 | break; |
ruschigo | 62:89df9529dbb0 | 123 | } |
ruschigo | 62:89df9529dbb0 | 124 | |
ruschigo | 62:89df9529dbb0 | 125 | if (circ_buffer[rd_index] == '>') |
ruschigo | 62:89df9529dbb0 | 126 | { |
ruschigo | 62:89df9529dbb0 | 127 | ///encontrou final de cmd |
ruschigo | 62:89df9529dbb0 | 128 | break; |
ruschigo | 62:89df9529dbb0 | 129 | } |
ruschigo | 62:89df9529dbb0 | 130 | |
ruschigo | 62:89df9529dbb0 | 131 | buf_len--; |
ruschigo | 62:89df9529dbb0 | 132 | rd_index++; |
ruschigo | 62:89df9529dbb0 | 133 | |
ruschigo | 62:89df9529dbb0 | 134 | if (rd_index >= sizeof(circ_buffer)) { |
ruschigo | 62:89df9529dbb0 | 135 | rd_index = 0; |
ruschigo | 62:89df9529dbb0 | 136 | } |
ruschigo | 62:89df9529dbb0 | 137 | } |
ruschigo | 62:89df9529dbb0 | 138 | |
ruschigo | 62:89df9529dbb0 | 139 | /// Agora posso limpar todo o buffer circular |
ruschigo | 62:89df9529dbb0 | 140 | rd_index = 0; |
ruschigo | 62:89df9529dbb0 | 141 | wr_index = 0; |
ruschigo | 62:89df9529dbb0 | 142 | buf_len = 0; |
ruschigo | 62:89df9529dbb0 | 143 | |
ruschigo | 62:89df9529dbb0 | 144 | /// A mensagem foi truncada entao sai. |
ruschigo | 62:89df9529dbb0 | 145 | if (msg_len == 0) { |
ruschigo | 62:89df9529dbb0 | 146 | pc.attach(&serial_post_to_queue, RawSerial::RxIrq); |
ruschigo | 62:89df9529dbb0 | 147 | return; |
ruschigo | 62:89df9529dbb0 | 148 | } |
ruschigo | 62:89df9529dbb0 | 149 | |
ruschigo | 62:89df9529dbb0 | 150 | /// Troco o '>' do final por null |
ruschigo | 62:89df9529dbb0 | 151 | msg[msg_len - 1] = '\0'; |
ruschigo | 62:89df9529dbb0 | 152 | |
ruschigo | 62:89df9529dbb0 | 153 | SerialCommandRun(msg); |
ruschigo | 62:89df9529dbb0 | 154 | |
ruschigo | 62:89df9529dbb0 | 155 | pc.attach(&serial_post_to_queue, RawSerial::RxIrq); |
ruschigo | 62:89df9529dbb0 | 156 | return; |
ruschigo | 62:89df9529dbb0 | 157 | } |
ruschigo | 64:ed68ddac6360 | 158 | |
ruschigo | 64:ed68ddac6360 | 159 | |
ruschigo | 64:ed68ddac6360 | 160 | void PrintDebugMsg(char *msg){ |
ruschigo | 64:ed68ddac6360 | 161 | pc.printf(msg); |
ruschigo | 64:ed68ddac6360 | 162 | } |