85 lines
2.2 KiB
C
85 lines
2.2 KiB
C
#include "MyTimer.h"
|
|
#include "stm32f10x.h"
|
|
#include "Driver_GPIO.h"
|
|
#include "Plateau.h"
|
|
#include "Telecommande.h"
|
|
#define NULL 0
|
|
|
|
/* Declaration */
|
|
USART_TypeDef USART ;
|
|
USART_TypeDef * Usart = &USART;
|
|
MyGPIO_Struct_TypeDef GPIOA10; // pin rx = reception
|
|
MyGPIO_Struct_TypeDef GPIOA9; // pin tx = envoie
|
|
void (* ptr) (char) = NULL;
|
|
|
|
/* Configuration de l'UART */
|
|
void Init_USART (USART_TypeDef * USART){
|
|
|
|
/* Configuration des GPIOs */
|
|
GPIOA10.GPIO = GPIOA;
|
|
GPIOA10.GPIO_Pin = 10;
|
|
GPIOA10.GPIO_Conf = In_Floating;
|
|
MyGPIO_Init(&GPIOA10);
|
|
|
|
GPIOA9.GPIO = GPIOA;
|
|
GPIOA9.GPIO_Pin = 9;
|
|
GPIOA9.GPIO_Conf = AltOut_Ppull;
|
|
MyGPIO_Init(&GPIOA9);
|
|
|
|
/* Configuration de l'USART */
|
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN ; // activation de la clock
|
|
USART1->CR1 |= USART_CR1_UE ; // rendre usart enable
|
|
USART1->CR1 &= ~(USART_CR1_M); // mettre à 8 la longueur du mot
|
|
|
|
// bd 9600 (cf. prof) puis calcul de la mantisse et de la fraction (cf. p 804)
|
|
USART1->BRR |= 0x0C; // mantisse
|
|
USART1->BRR |= (0x01D4 << 4) ; // fraction
|
|
|
|
// Active la reception et l'emission
|
|
USART1->CR1 |= USART_CR1_RE;
|
|
USART1->CR1 |= USART_CR1_TE;
|
|
|
|
/* On active les interruptions pour l'usart1 */
|
|
USART1->CR1 |= USART_CR1_RXNEIE;
|
|
}
|
|
|
|
/* Envoie d'un character */
|
|
void Send_Char (char c) {
|
|
while (((USART1->SR >> 6) & 0x1) == 0);// tant que la transmission n'est pas finie
|
|
USART1->DR = c;
|
|
}
|
|
|
|
/* Envoie de messages */
|
|
void Send_Message (char message[]){
|
|
int i = 0;
|
|
while (message[i] != '\0') {
|
|
Send_Char(message[i]);
|
|
i+=1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void USART1_IRQHandler() {
|
|
char input;
|
|
input = USART1->DR ; // on récupère ce qu'il y a dans le registre
|
|
(*ptr) (input); // on execute la fonction
|
|
}
|
|
|
|
/* Reception de message */
|
|
void Init_Message_Reception(void (* pFonction) (char)){ //(void (* pFonction) (char)){
|
|
|
|
/* Configuration de l'interruption */
|
|
//!\\ A revoir cette partie ! p 198
|
|
NVIC->ISER[1] |= NVIC_ISER_SETENA_5 ;//NVIC_IABR_ACTIVE_5 ; // on autorise l'interuption au niveau du coeur par le usart1
|
|
NVIC->IP[37] = 7; // on parametre la prio de l'usart
|
|
|
|
/* Activation de la réception */
|
|
USART1->CR1 |= USART_CR1_RE ;
|
|
ptr = pFonction; //fonction à appeler par le handler
|
|
|
|
|
|
}
|
|
|
|
|
|
|