00001
00029
00030
00031 #include "stm32f10x.h"
00032
00033 #include "roboot_if_comm.h"
00034 #include "config.h"
00035
00036
00037 #if BOOTLOADER_USART == USART_1
00038 #define USARTx USART1
00039 #elif BOOTLOADER_USART == USART_2
00040 #define USARTx USART2
00041 #elif BOOTLOADER_USART == USART_3
00042 #define USARTx USART3
00043 #else
00044 #error "Wrong USART configured"
00045 #endif
00046
00047
00048
00049 void CommHwInit(void)
00050 {
00051 USART_InitTypeDef USART_InitStructure;
00052 GPIO_InitTypeDef gpio_data;
00053
00054
00055 #if BOOTLOADER_USART == USART_1
00056 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
00057 #elif BOOTLOADER_USART == USART_2
00058 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
00059 #elif BOOTLOADER_USART == USART_3
00060 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE );
00061 #endif
00062
00063 USART_InitStructure.USART_BaudRate = BOOTLOADER_USART_SPEED;
00064 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00065 #if BOOTLOADER_USART_STOPBITS == 1
00066 USART_InitStructure.USART_StopBits = USART_StopBits_1;
00067 #elif BOOTLOADER_USART_STOPBITS == 2
00068 USART_InitStructure.USART_StopBits = USART_StopBits_2;
00069 #else
00070 #error "Wrong configuration for number of USART stop bits"
00071 #endif
00072 #if BOOTLOADER_USART_PARITY == USART_PARITY_NONE
00073 USART_InitStructure.USART_Parity = USART_Parity_No;
00074 #elif BOOTLOADER_USART_PARITY == USART_PARITY_EVEN
00075 USART_InitStructure.USART_Parity = USART_Parity_Even;
00076 #elif BOOTLOADER_USART_PARITY == USART_PARITY_ODD
00077 USART_InitStructure.USART_Parity = USART_Parity_Odd;
00078 #else
00079 #error "Wrong configuration for USART parity control"
00080 #endif
00081 #if BOOTLOADER_USART_FC == USART_FC_NONE
00082 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00083 #elif BOOTLOADER_USART_FC == USART_FC_RTS
00084 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS;
00085 #elif BOOTLOADER_USART_FC == USART_FC_CTS
00086 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_CTS;
00087 #elif BOOTLOADER_USART_FC == USART_FC_RTS_CTS
00088 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
00089 #else
00090 #error "Wrong configuration for USART parity control"
00091 #endif
00092 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00093
00094 USART_Init(USARTx, &USART_InitStructure);
00095 USART_Cmd(USARTx, ENABLE);
00096
00097
00098 GPIO_StructInit(&gpio_data);
00099
00100 #if BOOTLOADER_USART == USART_1
00101
00102 gpio_data.GPIO_Pin = GPIO_Pin_9;
00103 #if (BOOTLOADER_USART_FC == USART_FC_RTS) || (BOOTLOADER_USART_FC == USART_FC_RTS_CTS)
00104
00105 gpio_data.GPIO_Pin |= GPIO_Pin_12;
00106 #endif
00107 gpio_data.GPIO_Mode = GPIO_Mode_AF_PP;
00108 GPIO_Init(GPIOA, &gpio_data);
00109
00110
00111 gpio_data.GPIO_Pin = GPIO_Pin_10;
00112 #if (BOOTLOADER_USART_FC == USART_FC_CTS) || (BOOTLOADER_USART_FC == USART_FC_RTS_CTS)
00113
00114 gpio_data.GPIO_Pin |= GPIO_Pin_11;
00115 #endif
00116 gpio_data.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00117 GPIO_Init(GPIOA, &gpio_data);
00118
00119 #elif BOOTLOADER_USART == USART_2
00120
00121 gpio_data.GPIO_Pin = GPIO_Pin_2;
00122 #if (BOOTLOADER_USART_FC == USART_FC_RTS) || (BOOTLOADER_USART_FC == USART_FC_RTS_CTS)
00123
00124 gpio_data.GPIO_Pin |= GPIO_Pin_1;
00125 #endif
00126 gpio_data.GPIO_Mode = GPIO_Mode_AF_PP;
00127 GPIO_Init(GPIOA, &gpio_data);
00128
00129
00130 gpio_data.GPIO_Pin = GPIO_Pin_3;
00131 #if (BOOTLOADER_USART_FC == USART_FC_CTS) || (BOOTLOADER_USART_FC == USART_FC_RTS_CTS)
00132
00133 gpio_data.GPIO_Pin |= GPIO_Pin_0;
00134 #endif
00135 gpio_data.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00136 GPIO_Init(GPIOA, &gpio_data);
00137
00138 #elif BOOTLOADER_USART == USART_3
00139
00140
00141 gpio_data.GPIO_Pin = GPIO_Pin_10;
00142 #if (BOOTLOADER_USART_FC == USART_FC_RTS) || (BOOTLOADER_USART_FC == USART_FC_RTS_CTS)
00143
00144 gpio_data.GPIO_Pin |= GPIO_Pin_14;
00145 #endif
00146 gpio_data.GPIO_Mode = GPIO_Mode_AF_PP;
00147 GPIO_Init(GPIOB, &gpio_data);
00148
00149
00150 gpio_data.GPIO_Pin = GPIO_Pin_11;
00151 #if (BOOTLOADER_USART_FC == USART_FC_CTS) || (BOOTLOADER_USART_FC == USART_FC_RTS_CTS)
00152
00153 gpio_data.GPIO_Pin |= GPIO_Pin_13;
00154 #endif
00155 gpio_data.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00156 GPIO_Init(GPIOB, &gpio_data);
00157 #endif
00158
00159 }
00160
00161
00162
00163
00164 void CommHwDeinit(void)
00165 {
00166 USART_DeInit(USARTx);
00167 }
00168
00169
00170
00171 void CommHwSendByte(uint8_t c)
00172 {
00173
00174 while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
00175 {
00176 ;
00177 }
00178 USART_SendData(USARTx, c);
00179 }
00180
00181
00182
00183
00184 int16_t CommHwGetByte(void)
00185 {
00186 if (USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == RESET)
00187 {
00188 return -1;
00189 }
00190 else
00191 {
00192 return (int16_t) (USART_ReceiveData(USARTx) & 0xFFU);
00193 }
00194 }
00195
00196
00197
00198 void MCUBusyDelay(void)
00199 {
00200 uint32_t delay;
00201 delay = 120UL;
00202
00203 while (delay--)
00204 {
00205 __asm volatile
00206 (
00207 " nop \n"
00208 " nop \n"
00209 " nop \n"
00210 );
00211 }
00212 }
00213