plat.c

Go to the documentation of this file.
00001 
00010 #undef __FILE_ID__
00011 #define __FILE_ID__ 0x70
00012 
00013 #include <stdio.h>
00014 #include "stm32f10x.h"
00015 #include "platform_config.h"
00016 #include "pm.h"
00017 
00018 void RCC_Configuration(void);
00019 void GPIO_Configuration(void);
00020 void NVIC_Configuration(void);
00021 void RTC_Configuration(void);
00022 
00023 void
00024 RCC_Configuration(void)
00025 {
00026     SystemInit();
00027 
00028     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx | RCC_APB2Periph_AFIO, ENABLE);
00029 
00030     /* Enable USART1 clock */
00031     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
00032 }
00033 
00034 
00035 void
00036 GPIO_Configuration(void)
00037 {
00038     GPIO_InitTypeDef GPIO_InitStructure;
00039 
00040     /* Configure USARTx_Tx as alternate function push-pull */
00041     GPIO_InitStructure.GPIO_Pin = GPIO_TxPin;
00042     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00043     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00044     GPIO_Init(GPIOx, &GPIO_InitStructure);
00045 
00046     /* Configure USARTx_Rx as input floating */
00047     GPIO_InitStructure.GPIO_Pin = GPIO_RxPin;
00048     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00049     GPIO_Init(GPIOx, &GPIO_InitStructure);
00050 }
00051 
00052 
00053 void
00054 NVIC_Configuration(void)
00055 {
00056     NVIC_InitTypeDef NVIC_InitStructure;
00057 
00058     /* Configure one bit for preemption priority */
00059     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
00060 
00061     /* Enable the RTC Interrupt */
00062     NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
00063     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
00064     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00065     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00066     NVIC_Init(&NVIC_InitStructure);
00067 }
00068 
00069 
00074 void
00075 RTC_Configuration(void)
00076 {
00077     /* Enable PWR and BKP clocks */
00078     RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
00079 
00080     /* Allow access to BKP Domain */
00081     PWR_BackupAccessCmd(ENABLE);
00082 
00083     /* Reset Backup Domain */
00084     BKP_DeInit();
00085 
00086     /* Enable LSE */
00087     RCC_LSEConfig(RCC_LSE_ON);
00088     /* Wait till LSE is ready */
00089     while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
00090     {}
00091 
00092     /* Select LSE as RTC Clock Source */
00093     RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
00094 
00095     /* Enable RTC Clock */
00096     RCC_RTCCLKCmd(ENABLE);
00097 
00098     /* Wait for RTC registers synchronization */
00099     RTC_WaitForSynchro();
00100 
00101     /* Wait until last write operation on RTC registers has finished */
00102     RTC_WaitForLastTask();
00103 
00104     /* Enable the RTC Second */
00105     RTC_ITConfig(RTC_IT_SEC, ENABLE);
00106 
00107     /* Wait until last write operation on RTC registers has finished */
00108     RTC_WaitForLastTask();
00109 
00110     /* Set RTC prescaler: set RTC period to 1sec */
00111     RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
00112 
00113     /* Wait until last write operation on RTC registers has finished */
00114     RTC_WaitForLastTask();
00115 }
00116 
00117 
00118 /*
00119  * Retargets the C library printf function to the USART.
00120  */
00121 int fputc(int ch, FILE *f)
00122 {
00123     plat_putByte((uint8_t) ch);
00124     return ch;
00125 }
00126 
00127 
00128 PmReturn_t
00129 plat_init(void)
00130 {
00131     USART_InitTypeDef USART_InitStructure;
00132 
00133 #ifdef DEBUG
00134     debug();
00135 #endif
00136 
00137     RCC_Configuration();
00138     NVIC_Configuration();
00139     GPIO_Configuration();
00140     RTC_Configuration();
00141 
00142     USART_InitStructure.USART_BaudRate = UART_BAUD;
00143     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00144     USART_InitStructure.USART_StopBits = USART_StopBits_1;
00145     USART_InitStructure.USART_Parity = USART_Parity_No ;
00146     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00147     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00148 
00149     /* Configure the USARTx */
00150     USART_Init(USARTx, &USART_InitStructure);
00151 
00152     /* Enable the USARTx */
00153     USART_Cmd(USARTx, ENABLE);
00154 
00155     return PM_RET_OK;
00156 }
00157 
00158 
00159 /* TODO: disable the peripherals and interrupts */
00160 PmReturn_t
00161 plat_deinit(void)
00162 {
00163     return PM_RET_OK;
00164 }
00165 
00166 
00167 /*
00168  * Gets a byte from the address in the designated memory space
00169  * Post-increments *paddr.
00170  */
00171 uint8_t
00172 plat_memGetByte(PmMemSpace_t memspace, uint8_t const **paddr)
00173 {
00174     uint8_t b = 0;
00175 
00176     switch (memspace)
00177     {
00178         case MEMSPACE_RAM:
00179         case MEMSPACE_PROG:
00180             b = **paddr;
00181             *paddr += 1;
00182             return b;
00183         case MEMSPACE_EEPROM:
00184         case MEMSPACE_SEEPROM:
00185         case MEMSPACE_OTHER0:
00186         case MEMSPACE_OTHER1:
00187         case MEMSPACE_OTHER2:
00188         case MEMSPACE_OTHER3:
00189         default:
00190             return 0;
00191     }
00192 }
00193 
00194 
00195 PmReturn_t
00196 plat_getByte(uint8_t *b)
00197 {
00198     int c;
00199     PmReturn_t retval = PM_RET_OK;
00200 
00201     while(USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == RESET)
00202     {
00203     }
00204 
00205     c = (int) USART_ReceiveData(USARTx);
00206     *b = c & 0xFF;
00207 
00208     if (c > 0xFF)
00209     {
00210         PM_RAISE(retval, PM_RET_EX_IO);
00211     }
00212 
00213     return retval;
00214 }
00215 
00216 
00217 PmReturn_t
00218 plat_putByte(uint8_t b)
00219 {
00220     while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
00221     {
00222     }
00223 
00224     USART_SendData(USARTx, (uint8_t) (b & 0x1FF));
00225 
00226     return PM_RET_OK;
00227 }
00228 
00229 
00230 PmReturn_t
00231 plat_getMsTicks(uint32_t *r_ticks)
00232 {
00233     /* TODO: make access atomic */
00234     *r_ticks = pm_timerMsTicks;
00235 
00236     return PM_RET_OK;
00237 }
00238 
00239 
00240 void
00241 plat_reportError(PmReturn_t result)
00242 {
00243      /* Print error */
00244     printf("Error:     0x%02X\n", result);
00245     printf("  Release: 0x%02X\n", gVmGlobal.errVmRelease);
00246     printf("  FileId:  0x%02X\n", gVmGlobal.errFileId);
00247     printf("  LineNum: %d\n", gVmGlobal.errLineNum);
00248 
00249     /* Print traceback */
00250     {
00251         pPmObj_t pframe;
00252         pPmObj_t pstr;
00253         PmReturn_t retval;
00254 
00255         printf("Traceback (top first):\n");
00256 
00257         /* Get the top frame */
00258         pframe = (pPmObj_t)gVmGlobal.pthread->pframe;
00259 
00260         /* If it's the native frame, print the native function name */
00261         if (pframe == (pPmObj_t)&(gVmGlobal.nativeframe))
00262         {
00263 
00264             /* The last name in the names tuple of the code obj is the name */
00265             retval = tuple_getItem((pPmObj_t)gVmGlobal.nativeframe.nf_func->
00266                                    f_co->co_names, -1, &pstr);
00267             if ((retval) != PM_RET_OK)
00268             {
00269                 printf("  Unable to get native func name.\n");
00270                 return;
00271             }
00272             else
00273             {
00274                 printf("  %s() __NATIVE__\n", ((pPmString_t)pstr)->val);
00275             }
00276 
00277             /* Get the frame that called the native frame */
00278             pframe = (pPmObj_t)gVmGlobal.nativeframe.nf_back;
00279         }
00280 
00281         /* Print the remaining frame stack */
00282         for (;
00283              pframe != C_NULL;
00284              pframe = (pPmObj_t)((pPmFrame_t)pframe)->fo_back)
00285         {
00286             /* The last name in the names tuple of the code obj is the name */
00287             retval = tuple_getItem((pPmObj_t)((pPmFrame_t)pframe)->
00288                                    fo_func->f_co->co_names, -1, &pstr);
00289             if ((retval) != PM_RET_OK) break;
00290 
00291             printf("  %s()\n", ((pPmString_t)pstr)->val);
00292         }
00293         printf("  <module>.\n");
00294     }
00295 }

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9